{"id":278,"date":"2020-08-05T12:24:23","date_gmt":"2020-08-05T11:24:23","guid":{"rendered":"https:\/\/blog.yryo.net\/en\/?p=278"},"modified":"2021-01-18T23:00:34","modified_gmt":"2021-01-18T23:00:34","slug":"enforcing-latex-macro-expansion-orders-and-unwritten-rules-about-formatting-a-macro","status":"publish","type":"post","link":"https:\/\/blog.yryo.net\/en\/?p=278","title":{"rendered":"Enforcing LaTeX macro expansion orders and unwritten rules about formatting a macro"},"content":{"rendered":"\n<p>LaTeX document is actually a program, with text to be formatted. However they are not particularly friendly to program on. At least it takes a lot of trial and error&#8230; Here is how I tackled a problem with working with macro in LaTeX.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Problem:<\/h2>\n\n\n\n<p>I recently had to generate a rather complex table with colouring rules. (essentially a &#8216;heatmap&#8217; table) To do this, I have used several packages: <code>\\colortbl<\/code>, <code>\\color<\/code> and <code>\\fp<\/code>. <\/p>\n\n\n\n<p><code>colortbl<\/code> and <code>color<\/code> was used to set the colour of the cell itself using <code>\\cellcolor{}<\/code> command. In addition, <code>fp<\/code> was needed to compute a colour based on the value by simply doing &#8216;1 &#8211; decimal value&#8217; to get the ratio between the two colours in RGB. While LaTeX document is a program, is really not designed for us, endusers, to write a program that requires computation. By default, decimal point calculation is a nightmare, thus <code>fp<\/code> was imported to make it less painful. <\/p>\n\n\n\n<p> One of the troublesome aspect is how LaTeX documents are compiled. The gist of this is basically a series of token expansion. It expands top to bottom, left to right, through a series of commands. And in the context of macros, something like this becomes a nightmare:<\/p>\n\n\n\n<pre class=\"wp-block-verse\">  \\cellcolor[rgb]{\\mycommand{#1},0,0}  <\/pre>\n\n\n\n<p>Something like this will probably cause an error indicating this is an invalid argument for <code>\\cellcolor{}<\/code> as they expect three values in the range of 0.0-1.0 representing red, green, and blue. <br>To solve this, you need to prevent LaTeX compiler to attempt expanding <code> \\cellcolor{}<\/code> before <code> \\mycommand <\/code>. My initial attempt was to insert <code>\\expandafter<\/code> to make LaTeX expand the later tokens to expand first. However, that did not solve this.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Solution:<\/h2>\n\n\n\n<p>The solution ended up being rather hacky but almost similar to C macro:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\\edef\\tmp{\\noexpand\\cellcolor[rgb]{\\mycommand{#1},0,0}}%\n\\tmp% <\/pre>\n\n\n\n<p>UPDATE: The solution (I believe) does the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Define temporary macro using <code>\\edef<\/code><\/li><li>Avoid the expansion of<code> \\cellcolor<\/code> by prepending <code>\\noexpand<\/code> when <code>\\edef<\/code> is being expanded to define a macro<\/li><li>But that did not stop expanding<code> \\mycommand<\/code> <\/li><li>Calls the temporary macro defined with the result of <code>\\mycommand<\/code> inserted <\/li><\/ul>\n\n\n\n<p>I don\u2019t claim to understand perfectly how it was expanded, but I believe this is what has happened. This idea came from [2]. In addition, I found [1] to be useful in understanding how LaTeX parses the commands. (I also saw a great table summarising different commands used to instruct expansion order, which now I can\u2019t find&#8230;)<\/p>\n\n\n\n<p>In the process of trying this, I have also discovered that macros should have escapes for new lines (<code>\\n<\/code>) as part of the macros by appending <code>%<\/code> at the end of each lines. As similar to macros, it will paste the whole block when being expanded. This lead to an interesting problem in the context of <code>\\tabular{}<\/code> command where the text inside the cell had an odd padding in the cell, breaking centering column alignment, which was caused by this new lines interpreted and inserted. Thus if you want a sensible looking macros in LaTeX, where not <em>everything<\/em> is in a single line, end of line has to be escaped.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">In short:<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>If <code>\\expandafter<\/code> fails, try defining a temporary macro and use <code>\\noexpand<\/code>.<\/li><li>Remember to escape the end of line when changing line in your multi-line LaTeX macro by using <code>%<\/code>.<\/li><\/ul>\n\n\n\n<p>References:<br>[1] <a href=\"https:\/\/www.overleaf.com\/learn\/latex\/Articles\/How_does_%5Cexpandafter_work:_An_introduction_to_TeX_tokens\">Overleaf &#8211; How does \\expandafter work: An introduction to TeX tokens<\/a><br>https:\/\/www.overleaf.com\/learn\/latex\/Articles\/How_does_%5Cexpandafter_work:_An_introduction_to_TeX_tokens<br>[2] <a href=\"http:\/\/www.tug.org\/TUGboat\/Articles\/tb09-1\/tb20bechtolsheim.pdf\">A Tutorial on \\expandafter &#8211; Stephan v. Bechtolsheim<\/a> http:\/\/www.tug.org\/TUGboat\/Articles\/tb09-1\/tb20bechtolsheim.pdf<\/p>\n\n\n\n<p>UPDATE (2020 08 05): Added bit of explanation of the solution. Also fixed some typos&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>LaTeX document is actually a program, with text to be formatted. However they are not particularly friendly to program on. At least it takes a lot of trial and error&#8230; Here is how I tackled a problem with working with macro in LaTeX.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[9],"tags":[43,44],"class_list":["post-278","post","type-post","status-publish","format-standard","hentry","category-bits-and-bytes","tag-latex","tag-macro"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8IT5l-4u","_links":{"self":[{"href":"https:\/\/blog.yryo.net\/en\/index.php?rest_route=\/wp\/v2\/posts\/278","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.yryo.net\/en\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.yryo.net\/en\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.yryo.net\/en\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.yryo.net\/en\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=278"}],"version-history":[{"count":17,"href":"https:\/\/blog.yryo.net\/en\/index.php?rest_route=\/wp\/v2\/posts\/278\/revisions"}],"predecessor-version":[{"id":323,"href":"https:\/\/blog.yryo.net\/en\/index.php?rest_route=\/wp\/v2\/posts\/278\/revisions\/323"}],"wp:attachment":[{"href":"https:\/\/blog.yryo.net\/en\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.yryo.net\/en\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.yryo.net\/en\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}