<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel>
        <title>Enter the void *</title>
        <link>http://blog.emillon.org</link>
        <description><![CDATA[Yet another random hacker]]></description>
        <atom:link href="http://blog.emillon.org/feeds/tex.xml" rel="self"
                   type="application/rss+xml" />
        <lastBuildDate>Thu, 05 Jun 2014 00:00:00 UT</lastBuildDate>
        <item>
    <title>Bring your own switch</title>
    <link>http://blog.emillon.org/posts/2014-06-05-bring-your-own-switch.html</link>
    <description><![CDATA[<p>TeX is a very primitive language. Everything is dynamic, even parsing. This
explains in part why it’s so long to compile.</p>
<p>It also means that it’s very flexible : it’s possible to define your own control
structures. Here is a small explanation of an implementation of a “switch” macro
I made last year. It is released as part of my
<a href="https://github.com/emillon/discotex">discotex</a> library (a collection of macros,
really).</p>
<p>We want to define a control structure that we can use in the following way:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode latex"><code class="sourceCode latex"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">\switch</span>{what}{case1}{then1}</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a>             {case2}{then2}</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a>             {case3}{then3}</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a>             {END}</span></code></pre></div>
<p>Then, if <code>what</code> is equal to <code>case1</code>, the whole construct evaluates to <code>then1</code>,
etc. This looks like a function with a variable number of arguments, but
actually this is well adapted to how TeX works.</p>
<p>In TeX, control is provided through macros, i.e. rules to rewrite text. Suppose
we want to do a macro  that expands to “x and y”. LaTeX users are
used to the following syntax:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode latex"><code class="sourceCode latex"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="fu">\newcommand</span>{<span class="ex">\couple</span>}[2]{#1 and #2}</span></code></pre></div>
<p>But in TeX this is written:</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode latex"><code class="sourceCode latex"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="fu">\def\couple</span>#1#2{#1 and #2}</span></code></pre></div>
<p>Which roughly means that after reading <code>\couple</code>, TeX will read two strings<a href="#fn1" class="footnote-ref" id="fnref1" role="doc-noteref"><sup>1</sup></a>
and bind them to <code>#1</code> and <code>#2</code> in the body. So <code>\couple{A}{B}</code> is expanded to <code>A and B</code>.</p>
<p>Here comes the trick used for defining variadic functions: if more arguments are
provided than the number of arguments at the definition point, the extra ones
are kept at their place. If fewer arguments are provided, the strings after the
call site will be used. So, one can look at TeX functions as just a system to
pop strings from the calling site.</p>
<p>Using this, we can implement <code>\switch</code>:</p>
<ul>
<li><p>After reading <code>\switch</code>, read two arguments so that we’re considering
<code>\switch{what}{x}</code>.</p>
<ul>
<li>If <code>x</code> is equal to <code>END</code>, it is an error: we did not find the entry. The
<code>END</code> string is not special to TeX, it is just a convention of our macro.</li>
</ul></li>
<li><p>Otherwise, pop one more string so that we’re considering
<code>\switch{what}{case}{then}</code>.</p>
<ul>
<li>If <code>what</code> is not equal to <code>case</code>, we have to recursively call
<code>\switch{what}</code> which will pop the rest.</li>
</ul></li>
<li><p>If <code>what</code> is equal to <code>case</code>, then the result is <code>then</code>. But it is not
enough to return it: we have to pop strings until <code>END</code> is reached.
Otherwise they would be output normally and put it the document.</p></li>
</ul>
<p>These 3 points map well to the final TeX code.</p>
<p>To read the first case, we write a function with only two parameters. For string
comparison we use <code>\ifstrequal{a}{b}{t}{f}</code><a href="#fn2" class="footnote-ref" id="fnref2" role="doc-noteref"><sup>2</sup></a> which expands to <code>t</code> if <code>a</code> and
<code>b</code> are equal, or <code>f</code> otherwise. Note that <code>\switch@next</code> is the name of a
function. In <code>.sty</code> files, it is possible to use <code>@</code> in symbol names. It is a
convention for private macros as they can not be directly used in <code>.tex</code> files.</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode latex"><code class="sourceCode latex"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="fu">\def\switch</span>#1#2{</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">\ifstrequal</span>{#2}{END}{</span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a>    <span class="fu">\errmessage</span>{switch : case &quot;#1&quot; not found}</span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a>  }{</span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a>    <span class="fu">\switch@next</span>{#1}{#2}</span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a>  }</span>
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
<p>It is also used to do the actual comparison and the recursive call.</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode latex"><code class="sourceCode latex"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="fu">\def\switch@next</span>#1#2#3{</span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">\ifstrequal</span>{#1}{#2}</span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a>    {#3<span class="fu">\switch@last</span>}</span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a>    {</span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a>      <span class="fu">\switch</span>{#1}</span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a>    }</span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
<p>Then <code>\switch@last</code> is a simple recursive function which simulates a loop.
Because the recursive call is done without an explicit parameter, it will keep
on popping strings until finding <code>END</code>.</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode latex"><code class="sourceCode latex"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="fu">\def\switch@last</span>#1{</span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">\ifstrequal</span>{#1}{END}{}</span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a>  {<span class="fu">\switch@last</span>}</span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
<p>That’s it, the macro works. <a href="https://github.com/emillon/discotex">You can even try it!</a></p>
<p>I am not sure that I would like to write more complex control structures but
this was useful to me both in writing it and using it. I hope that you enjoyed
it!</p>
<section id="footnotes" class="footnotes footnotes-end-of-document" role="doc-endnotes">
<hr />
<ol>
<li id="fn1"><p>I am not sure that this is the correct denomination. For example it will
read a string between curly braces, or a single character if they are omitted.
In that case it also eats whitespace, which is why you need stuff like <code>\xspace</code>
to prevent your macros from glueing string together.<a href="#fnref1" class="footnote-back" role="doc-backlink">↩︎</a></p></li>
<li id="fn2"><p>It is from the <code>etoolbox</code> package. How it works is an implementation
detail here, though it would probably be interesting.<a href="#fnref2" class="footnote-back" role="doc-backlink">↩︎</a></p></li>
</ol>
</section>]]></description>
    <pubDate>Thu, 05 Jun 2014 00:00:00 UT</pubDate>
    <guid>http://blog.emillon.org/posts/2014-06-05-bring-your-own-switch.html</guid>
    <dc:creator>Etienne Millon</dc:creator>
</item>

    </channel>
</rss>
