<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="4.4.1">Jekyll</generator><link href="https://xuc.me/blog.xml" rel="self" type="application/atom+xml" /><link href="https://xuc.me/" rel="alternate" type="text/html" /><updated>2026-04-24T01:05:39+00:00</updated><id>https://xuc.me/blog.xml</id><title type="html">CHENG XU</title><entry><title type="html">LaTeX Tutorial</title><link href="https://xuc.me/blog/latex-tutorial/" rel="alternate" type="text/html" title="LaTeX Tutorial" /><published>2019-09-26T00:00:00+00:00</published><updated>2019-09-26T00:00:00+00:00</updated><id>https://xuc.me/blog/LaTeX-Tutorial</id><content type="html" xml:base="https://xuc.me/blog/latex-tutorial/"><![CDATA[<p>I recently gave a tutorial on how to use the LaTeX. You can find the slides <a href="https://gh.xuc.me/latex-tutorial/slides.pdf">here</a>. To complement the tutorial slides, I also create an example project to highlight some LaTeX usages. The source code for both the slides and the example are available in <a href="https://github.com/xu-cheng/latex-tutorial">Github</a>.</p>]]></content><author><name></name></author><category term="LaTeX" /><summary type="html"><![CDATA[An Introduction to the LaTeX]]></summary></entry><entry><title type="html">Reuse TikZ Figures in Both Articles and Slides</title><link href="https://xuc.me/blog/reuse-tikz-figures-in-articles-and-slides/" rel="alternate" type="text/html" title="Reuse TikZ Figures in Both Articles and Slides" /><published>2019-06-20T00:00:00+00:00</published><updated>2019-06-20T00:00:00+00:00</updated><id>https://xuc.me/blog/Reuse-TikZ-Figures-in-Articles-and-Slides</id><content type="html" xml:base="https://xuc.me/blog/reuse-tikz-figures-in-articles-and-slides/"><![CDATA[<p>I use LaTeX a lot. I found it an effective tool to write articles, slides (using <a href="https://ctan.org/pkg/beamer">beamer</a>), and posters (using <a href="https://ctan.org/pkg/tikzposter">tikzposter</a>). Further more, I use <a href="https://www.ctan.org/pkg/pgf">TikZ/PGF</a> to draw all the figures. Not only the source code of the figure can be easily managed by version control software like git, TikZ is also well-integrated in beamer to create beautiful animations for slides.</p>

<p>It is common to have the same figure used in multiple places among articles, beamer slides, and posters. Therefore, it is desired to share the TikZ source codes without duplicating the files. Noted, the figures used in the beamer often contain the animations in the form of <code class="language-plaintext highlighter-rouge">\onslide&lt;overlay specification&gt;{...}</code>. As such, we need to a way to manage these differences in the TikZ source codes and avoid the duplication at the same time.</p>

<p>To tackle this issue, I found a nice solution is to use the <a href="https://www.ctan.org/pkg/standalone">standalone</a> package. As its name suggests, it can be used to create pictures as standalone or as part of a document. For example, we can create a figure as the following:</p>

<figure class="highlight"><pre><code class="language-latex" data-lang="latex"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre><span class="k">\documentclass</span><span class="na">[tikz]</span><span class="p">{</span>standalone<span class="p">}</span>

<span class="k">\usetikzlibrary</span><span class="p">{</span>calc,positioning<span class="p">}</span>

<span class="c">% Create fake \onslide and other commands for standalone picture</span>
<span class="k">\usepackage</span><span class="p">{</span>xparse<span class="p">}</span>
<span class="k">\NewDocumentCommand</span><span class="p">{</span><span class="k">\onslide</span><span class="p">}{</span>s t+ d&lt;&gt;<span class="p">}{}</span>
<span class="k">\NewDocumentCommand</span><span class="p">{</span><span class="k">\only</span><span class="p">}{</span>d&lt;&gt;<span class="p">}{}</span>
<span class="k">\NewDocumentCommand</span><span class="p">{</span><span class="k">\uncover</span><span class="p">}{</span>d&lt;&gt;<span class="p">}{}</span>
<span class="k">\NewDocumentCommand</span><span class="p">{</span><span class="k">\visible</span><span class="p">}{</span>d&lt;&gt;<span class="p">}{}</span>
<span class="k">\NewDocumentCommand</span><span class="p">{</span><span class="k">\invisible</span><span class="p">}{</span>d&lt;&gt;<span class="p">}{}</span>

<span class="nt">\begin{document}</span>

<span class="nt">\begin{tikzpicture}</span>
  <span class="k">\node</span><span class="na">[draw]</span> (start) <span class="p">{</span> Start <span class="p">}</span>;
  <span class="k">\node</span><span class="na">[draw, right=2cm of start]</span> (end) <span class="p">{</span> End <span class="p">}</span>;
  <span class="c">% The following animation will only have affect in beamer.</span>
  <span class="c">% In standalone mode, the figure is static.</span>
  <span class="k">\onslide</span>&lt;2-&gt; <span class="p">{</span> <span class="k">\draw</span><span class="na">[-latex]</span> (start) -- (end); <span class="p">}</span>
  <span class="k">\coordinate</span> (mid) at (<span class="p">$</span><span class="o">(</span><span class="nb">start</span><span class="o">)!</span><span class="nb">.</span><span class="m">5</span><span class="o">!(</span><span class="nb">end</span><span class="o">)</span><span class="p">$</span>);

  <span class="c">% We could control parts of figure only shown in beamer or vice versa.</span>
  <span class="k">\ifstandalone</span>
    <span class="k">\node</span><span class="na">[below=1cm of mid]</span> <span class="p">{</span>Only Shown in Standalone Figure<span class="p">}</span>;
  <span class="k">\else</span>
    <span class="k">\node</span><span class="na">[below=1cm of mid]</span> <span class="p">{</span>Only Shown in Beamer<span class="p">}</span>;
  <span class="k">\fi</span>
<span class="nt">\end{tikzpicture}</span>

<span class="nt">\end{document}</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>This file can be compiled directly to create standalone picture, which could be helpful if you want to create image used by web pages or other similar scenarios. To include this figure in the article or poster, you can use standalone package in the following manner:</p>

<figure class="highlight"><pre><code class="language-latex" data-lang="latex"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre><span class="k">\documentclass</span><span class="p">{</span>article<span class="p">}</span>

<span class="c">% `mode=build` tells LaTeX to build the image from the source then use it.</span>
<span class="k">\usepackage</span><span class="na">[mode=build]</span><span class="p">{</span>standalone<span class="p">}</span>

<span class="nt">\begin{document}</span>

<span class="nt">\begin{figure}</span>[t]
  <span class="k">\centering</span>
  <span class="k">\includestandalone</span><span class="na">[width=0.8\linewidth]</span><span class="p">{</span>./figure<span class="p">}</span> <span class="c">% without the `.tex` extension</span>
  <span class="k">\caption</span><span class="p">{</span>TikZ Figure in Article<span class="p">}</span>
<span class="nt">\end{figure}</span>

<span class="nt">\end{document}</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>Finally, this figure can be used in beamer with its animations functioning as normal.</p>

<figure class="highlight"><pre><code class="language-latex" data-lang="latex"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre><span class="k">\documentclass</span><span class="p">{</span>beamer<span class="p">}</span>

<span class="c">% `mode=tex` means we will use the source file of the figures directly.</span>
<span class="k">\usepackage</span><span class="na">[mode=tex]</span><span class="p">{</span>standalone<span class="p">}</span>

<span class="c">% We need to add TikZ definitions again. You could put them in a separated file</span>
<span class="c">% and use `\input{...}` command to avoid the duplication.</span>
<span class="k">\usepackage</span><span class="p">{</span>tikz<span class="p">}</span>
<span class="k">\usetikzlibrary</span><span class="p">{</span>calc,positioning<span class="p">}</span>

<span class="nt">\begin{document}</span>

<span class="nt">\begin{frame}</span><span class="p">{</span>TikZ Figure in Beamer<span class="p">}</span>
  <span class="nt">\begin{figure}</span>
    <span class="k">\centering</span>
    <span class="k">\includestandalone</span><span class="na">[width=0.8\linewidth]</span><span class="p">{</span>./figure<span class="p">}</span> <span class="c">% without the `.tex` extension</span>
  <span class="nt">\end{figure}</span>
<span class="nt">\end{frame}</span>

<span class="nt">\end{document}</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p><strong>Note</strong>: In order to use <code class="language-plaintext highlighter-rouge">mode=build</code>, you need to pass <code class="language-plaintext highlighter-rouge">-shell-escape</code> flag to the LaTeX compiler. Further, for <code class="language-plaintext highlighter-rouge">latexmk</code> users, <code class="language-plaintext highlighter-rouge">mode=buildnew</code> is preferred than <code class="language-plaintext highlighter-rouge">mode=build</code>. Otherwise, <code class="language-plaintext highlighter-rouge">latexmk</code> could be stuck in a loop of compiling the files repeatedly.</p>]]></content><author><name></name></author><category term="LaTeX" /><category term="TikZ" /><category term="Beamer" /><summary type="html"><![CDATA[I use LaTeX a lot. I found it an effective tool to write articles, slides (using beamer), and posters (using tikzposter). Further more, I use TikZ/PGF to draw all the figures. Not only the source code of the figure can be easily managed by version control software like git, TikZ is also well-integrated in beamer to create beautiful animations for slides.]]></summary></entry><entry><title type="html">Send Email in the Batch using Ruby</title><link href="https://xuc.me/blog/send-email-batch-ruby/" rel="alternate" type="text/html" title="Send Email in the Batch using Ruby" /><published>2018-04-25T00:00:00+00:00</published><updated>2018-04-25T00:00:00+00:00</updated><id>https://xuc.me/blog/Send-Email-Batch-Ruby</id><content type="html" xml:base="https://xuc.me/blog/send-email-batch-ruby/"><![CDATA[<p>Sometimes, you may find the need to send a large number of emails in batch. For example, you may be a TA of a course and are required to notify all the students of their grades. In this case, sending email in the batch would save lots of time and effort. In the following are my notes on how to achieve this using Ruby.</p>

<p>First, you need to install Ruby and <a href="https://github.com/mikel/mail">the mail library</a>. You can install the library by <code class="language-plaintext highlighter-rouge">gem install mail</code>.</p>

<p>Second, since there are lots of emails to be sent, it is a better idea to maintain a single SMTP connection to the email server to reduce the overhead. This is also beneficial to avoid possible network issues. You can open an SMTP connection using following Ruby codes, which connects to the email server in my department.</p>

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre><span class="nb">require</span> <span class="s2">"mail"</span>
<span class="nb">require</span> <span class="s2">"socket"</span>

<span class="n">smtp_conn</span> <span class="o">=</span> <span class="no">Net</span><span class="o">::</span><span class="no">SMTP</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="s2">"mh2.comp.hkbu.edu.hk"</span><span class="p">,</span> <span class="mi">465</span><span class="p">)</span>
<span class="n">smtp_conn</span><span class="p">.</span><span class="nf">set_debug_output</span> <span class="vg">$stderr</span>
<span class="n">smtp_conn</span><span class="p">.</span><span class="nf">enable_tls</span>
<span class="n">smtp_conn</span><span class="p">.</span><span class="nf">start</span> <span class="no">Socket</span><span class="p">.</span><span class="nf">gethostname</span><span class="p">,</span> <span class="s2">"&lt;username&gt;"</span><span class="p">,</span> <span class="s2">"&lt;password&gt;"</span><span class="p">,</span> <span class="s2">"login"</span>

<span class="no">Mail</span><span class="p">.</span><span class="nf">defaults</span> <span class="k">do</span>
  <span class="n">delivery_method</span> <span class="ss">:smtp_connection</span><span class="p">,</span> <span class="ss">connection: </span><span class="n">smtp_conn</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>Finally, format your emails and deliver them. It is worth noting that you should sleep a few seconds between each email delivering to avoid DoSing the email server.</p>

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre><span class="c1"># format the email</span>
<span class="n">mail</span> <span class="o">=</span> <span class="no">Mail</span><span class="p">.</span><span class="nf">new</span> <span class="k">do</span>
  <span class="n">from</span>     <span class="s1">'from@example.com'</span>
  <span class="n">to</span>       <span class="s1">'to@example.com'</span>
  <span class="n">subject</span>  <span class="s1">'&lt;subject&gt;'</span>
  <span class="n">body</span>     <span class="s1">'&lt;body&gt;'</span>
  <span class="n">add_file</span> <span class="ss">filename: </span><span class="s1">'some_attachment.txt'</span><span class="p">,</span> <span class="ss">content: </span><span class="no">File</span><span class="p">.</span><span class="nf">read</span><span class="p">(</span><span class="s1">'/path/to/attachment.txt'</span><span class="p">)</span>
<span class="k">end</span>

<span class="c1"># check the email</span>
<span class="nb">puts</span> <span class="n">mail</span>

<span class="c1"># send the email</span>
<span class="n">mail</span><span class="p">.</span><span class="nf">deliver!</span>

<span class="nb">sleep</span> <span class="mi">2</span> <span class="c1"># wait a bit</span>
<span class="c1"># handle next email</span>
</pre></td></tr></tbody></table></code></pre></figure>]]></content><author><name></name></author><category term="Ruby" /><summary type="html"><![CDATA[Sometimes, you may find the need to send a large number of emails in batch. For example, you may be a TA of a course and are required to notify all the students of their grades. In this case, sending email in the batch would save lots of time and effort. In the following are my notes on how to achieve this using Ruby.]]></summary></entry><entry><title type="html">Typesetting with LaTeX and R</title><link href="https://xuc.me/blog/latex-and-r/" rel="alternate" type="text/html" title="Typesetting with LaTeX and R" /><published>2017-03-29T00:00:00+00:00</published><updated>2017-03-29T00:00:00+00:00</updated><id>https://xuc.me/blog/LaTeX-and-R</id><content type="html" xml:base="https://xuc.me/blog/latex-and-r/"><![CDATA[<p>Sometimes, it is desirable to run R codes directly in your LaTeX document. For example, you may wish typeset your course assignment in LaTeX, in which the experiment results as well as figures can be computed by R. And the <a href="https://yihui.name/knitr/">knitr</a> package is one of the most elegant ways to fulfill such need.</p>

<p>There are many approaches to install R and the knitr package. Below are two possible ways.</p>

<ul>
  <li>Install R using your favorite package manager or directly from the <a href="https://www.r-project.org">official website</a>. And install knitr by running <code class="language-plaintext highlighter-rouge">install.packages('knitr', dependencies = TRUE)</code> in the R prompt.</li>
  <li>Install R using <a href="https://www.anaconda.com/download/">Anaconda</a> by running <code class="language-plaintext highlighter-rouge">conda install -c r r-essentials</code> in the shell. It will install R along with some most common used R libraries including the knitr. This approach is especially beneficial under the Windows operating system.</li>
</ul>

<p>Based on your favorite editor, different configuration can be implemented to setup for automatically and continues LaTeX compiling. You may refer to <a href="https://yihui.name/knitr/demo/editors/">this blog</a> for more information. However, what is missing is how to compile LaTeX with R (knitr) using <a href="https://www.ctan.org/pkg/latexmk/">latexmk</a>, which is one of the most widely used tools to compile LaTeX document and is supported by almost all of LaTeX editors.</p>

<p>Therefore, I wrote up below piece of code to allow latexmk work with the knitr files (<code class="language-plaintext highlighter-rouge">.Rnw</code> or <code class="language-plaintext highlighter-rouge">.Rtex</code>) seamlessly. You could append them in your global <code class="language-plaintext highlighter-rouge">~/.latexmkrc</code> file or local <code class="language-plaintext highlighter-rouge">.latexmkrc</code> under the project root.</p>

<figure class="highlight"><pre><code class="language-perl" data-lang="perl"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre><span class="c1"># only enable when compiling .Rnw or .Rtex file</span>
<span class="k">if</span><span class="p">(</span><span class="nb">grep</span><span class="p">(</span><span class="sr">/\.(rnw|rtex)$/i</span><span class="p">,</span> <span class="nv">@ARGV</span><span class="p">))</span> <span class="p">{</span>
    <span class="nv">$latex</span> <span class="o">=</span> <span class="p">'</span><span class="s1">internal knitrlatex </span><span class="p">'</span> <span class="o">.</span> <span class="nv">$latex</span><span class="p">;</span>
    <span class="nv">$pdflatex</span> <span class="o">=</span> <span class="p">'</span><span class="s1">internal knitrlatex </span><span class="p">'</span> <span class="o">.</span> <span class="nv">$pdflatex</span><span class="p">;</span>
    <span class="k">my</span> <span class="nv">$knitr_compiled</span> <span class="o">=</span> <span class="p">{};</span>
    <span class="k">sub </span><span class="nf">knitrlatex</span> <span class="p">{</span>
        <span class="k">for</span> <span class="p">(</span><span class="err">@</span><span class="nv">_</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">next</span> <span class="k">unless</span> <span class="o">-</span><span class="nv">e</span> <span class="vg">$_</span><span class="p">;</span>
            <span class="k">my</span> <span class="nv">$input</span> <span class="o">=</span> <span class="vg">$_</span><span class="p">;</span>
            <span class="k">next</span> <span class="k">unless</span> <span class="vg">$_</span> <span class="o">=~</span> <span class="sr">s/\.(rnw|rtex)$/.tex/i</span><span class="p">;</span>
            <span class="k">my</span> <span class="nv">$tex</span> <span class="o">=</span> <span class="vg">$_</span><span class="p">;</span>
            <span class="k">my</span> <span class="nv">$checksum</span> <span class="o">=</span> <span class="p">(</span><span class="nv">fdb_get</span><span class="p">(</span><span class="nv">$input</span><span class="p">))[</span><span class="o">-</span><span class="mi">1</span><span class="p">];</span>
            <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nv">$knitr_compiled</span><span class="p">{</span><span class="nv">$input</span><span class="p">}</span> <span class="o">||</span> <span class="nv">$knitr_compiled</span><span class="p">{</span><span class="nv">$input</span><span class="p">}</span> <span class="ow">ne</span> <span class="nv">$checksum</span><span class="p">)</span> <span class="p">{</span>
                <span class="k">my</span> <span class="nv">$ret</span> <span class="o">=</span> <span class="nb">system</span><span class="p">("</span><span class="s2">Rscript -e </span><span class="se">\"</span><span class="s2">knitr::knit('</span><span class="si">$input</span><span class="s2">')</span><span class="se">\"</span><span class="p">");</span>
                <span class="k">if</span><span class="p">(</span><span class="nv">$ret</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="nv">$ret</span><span class="p">;</span> <span class="p">}</span>
                <span class="nv">rdb_ensure_file</span><span class="p">(</span><span class="nv">$rule</span><span class="p">,</span> <span class="nv">$tex</span><span class="p">);</span>
                <span class="nv">$knitr_compiled</span><span class="p">{</span><span class="nv">$input</span><span class="p">}</span> <span class="o">=</span> <span class="nv">$checksum</span><span class="p">;</span>
            <span class="p">}</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="nb">system</span><span class="p">(</span><span class="err">@</span><span class="nv">_</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="c1"># clean up generated .tex file when running `latexmk -c &lt;root_file&gt;`</span>
    <span class="nv">$clean_ext</span> <span class="o">.=</span> <span class="p">'</span><span class="s1"> %R.tex</span><span class="p">';</span>
<span class="p">}</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p>For me, it works nicely under vim with the <a href="https://github.com/lervag/vimtex/">vimtex plugin</a>. Just name the file with <code class="language-plaintext highlighter-rouge">.Rnw</code> extension, and edit and compile it like normal LaTeX file.</p>]]></content><author><name></name></author><category term="LaTeX" /><category term="R" /><summary type="html"><![CDATA[Sometimes, it is desirable to run R codes directly in your LaTeX document. For example, you may wish typeset your course assignment in LaTeX, in which the experiment results as well as figures can be computed by R. And the knitr package is one of the most elegant ways to fulfill such need.]]></summary></entry><entry><title type="html">Pry in Interactive Homebrew Shell</title><link href="https://xuc.me/blog/pry-homebrew/" rel="alternate" type="text/html" title="Pry in Interactive Homebrew Shell" /><published>2015-02-09T00:00:00+00:00</published><updated>2015-02-09T00:00:00+00:00</updated><id>https://xuc.me/blog/Pry-Homebrew</id><content type="html" xml:base="https://xuc.me/blog/pry-homebrew/"><![CDATA[<p>Recently, I have been heavily involved to <a href="https://brew.sh">Homebrew</a>’s development. Homebrew itself offers an interactive
ruby shell, which can be handy for the development. You can use such tool by invoking <code class="language-plaintext highlighter-rouge">brew irb</code> in the shell. And as what its
name sounds, this tool is based on irb.</p>

<p>Personal, I found <a href="https://pry.github.io">pry</a> is much more power than irb. For example, Gregg Rothmeier wrote <a href="https://www.bignerdranch.com/blog/my-top-5-pry-features/">this blog</a> to show
some useful features pry provided. In addition, pry ships with the support of <a href="https://github.com/pry/pry/wiki/Available-plugins">plugin system</a>.</p>

<p>So, how to use pry as the front end of interactive Homberew shell? I wrote a piece of code as shown below. Put it in the file <code class="language-plaintext highlighter-rouge">brew-pry</code> and
make sure it has execute attribute and can be found in the <code class="language-plaintext highlighter-rouge">PATH</code>. That’s it. Run <code class="language-plaintext highlighter-rouge">brew pry</code> to use pry in Homebrew shell.</p>

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre><span class="c1">#!/usr/bin/env ruby</span>

<span class="vg">$:</span><span class="p">.</span><span class="nf">unshift</span> <span class="no">ENV</span><span class="p">[</span><span class="s2">"HOMEBREW_LIBRARY_PATH"</span><span class="p">]</span>
<span class="nb">require</span> <span class="s2">"global"</span>
<span class="nb">require</span> <span class="s2">"formula"</span>
<span class="nb">require</span> <span class="s2">"keg"</span>
<span class="nb">require</span> <span class="s2">"pry"</span>

<span class="k">class</span> <span class="nc">Symbol</span>
  <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">)</span>
    <span class="no">Formulary</span><span class="p">.</span><span class="nf">factory</span><span class="p">(</span><span class="nb">to_s</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">)</span>
  <span class="k">end</span>
<span class="k">end</span>
<span class="k">class</span> <span class="nc">String</span>
  <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">)</span>
    <span class="no">Formulary</span><span class="p">.</span><span class="nf">factory</span><span class="p">(</span><span class="nb">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">)</span>
  <span class="k">end</span>
<span class="k">end</span>

<span class="k">if</span> <span class="no">ARGV</span><span class="p">.</span><span class="nf">include?</span> <span class="s2">"--examples"</span>
  <span class="nb">puts</span> <span class="s2">"'v8'.f # =&gt; instance of the v8 formula"</span>
  <span class="nb">puts</span> <span class="s2">":hub.f.installed?"</span>
  <span class="nb">puts</span> <span class="s2">":lua.f.methods - 1.methods"</span>
  <span class="nb">puts</span> <span class="s2">":mpd.f.recursive_dependencies.reject(&amp;:installed?)"</span>
<span class="k">else</span>
  <span class="n">ohai</span> <span class="s2">"Interactive Homebrew Shell"</span>
  <span class="nb">puts</span> <span class="s2">"Example commands available with: brew pry --examples"</span>
  <span class="no">Pry</span><span class="p">.</span><span class="nf">start</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></figure>

<p><strong>Update (2015/2/16)</strong></p>

<p>I found using <code class="language-plaintext highlighter-rouge">HOMEBREW_LIBRARY_PATH</code> to locate Homebrew library path is a better idea.
This can be useful if you have multi Homebrew in your system.</p>]]></content><author><name></name></author><category term="Homebrew" /><summary type="html"><![CDATA[Recently, I have been heavily involved to Homebrew’s development. Homebrew itself offers an interactive ruby shell, which can be handy for the development. You can use such tool by invoking brew irb in the shell. And as what its name sounds, this tool is based on irb.]]></summary></entry><entry><title type="html">Rendering LaTeX using KaTeX and Jekyll</title><link href="https://xuc.me/blog/katex-and-jekyll/" rel="alternate" type="text/html" title="Rendering LaTeX using KaTeX and Jekyll" /><published>2014-12-06T00:00:00+00:00</published><updated>2014-12-06T00:00:00+00:00</updated><id>https://xuc.me/blog/KaTeX-and-Jekyll</id><content type="html" xml:base="https://xuc.me/blog/katex-and-jekyll/"><![CDATA[<p>So as a Ph.D. student who studies in Computer Science, using LaTeX to render the equations in my blog is inevitable. In general, I hope the render can support the following features:</p>

<ul>
  <li><strong>Markdown Support:</strong> I use Jekyll and markdown to write the blogs. So the render should be integrated to this system seamlessly. Moreover, it should be able to:
    <ul>
      <li>Use LaTeX code directly in the markdown without any other HTML/JavaScript notion. For example, <code class="language-plaintext highlighter-rouge">$$ E = mc^2 $$</code> should be rendered to <span class="kdmath">$E = mc^2$</span> automatically.</li>
      <li>Work in default Github Page environment. I want to use Github Page to automatically generate the site. And due to the security reason, <a href="https://jekyllrb.com/docs/plugins/">Github doesn’t allow running custom plugins for Jekyll</a>. Therefore, the rendering shouldn’t depend any non-default plugin or markdown parser.</li>
    </ul>
  </li>
  <li><strong>Quality:</strong> Quality is important. The render needs to be fast and output print quality typesetting.</li>
</ul>

<p>The most popular way to render LaTeX within Jekyll is using <a href="https://www.mathjax.org">MathJaX</a>. There are lots of blogs (e.g. <a href="https://www.gastonsanchez.com/visually-enforced/opinion/2014/02/16/Mathjax-with-jekyll/">this</a>) to show you how to configure it. But MathJaX has a huge shortage on rendering speed. It’s slow, not for a bit, but so slow that you can see the web page refreshing itself.</p>

<p>Recently, I came across a new project <a href="https://khan.github.io/KaTeX/">KaTeX</a>. It’s fast as you can see the comparison below in which KaTeX is on the left and MathJax is on the right.</p>

<center><img src="https://raw.githubusercontent.com/Khan/KaTeX/cae400581080ac317fda57b8b6ae6c7834a9ea93/img/katex-comparison.gif" width="80%" alt="KaTeX vs Mathjax" /></center>

<p>So how to make KaTeX work in the Jekyll? <a href="https://web.archive.org/web/20170117172154/http://willdrevo.com/latex-equation-rendering-in-javascript-with-jekyll-and-katex/">Will Drevo</a> offers a comprehensive manual to make it work. However, it requires us to write LaTeX code inside HTML <code class="language-plaintext highlighter-rouge">div</code> block. And I found <a href="https://www.intmath.com/blog/mathematics/katex-a-new-way-to-display-math-on-the-web-9445">this blog</a> to explain how to use KaTeX with JavaScript. Combining both of these works, and with some extra effort, I find the approach to use KaTeX seamlessly inside Jekyll.</p>

<p>First of all, we need to configure Jekyll using Kramdown as markdown parser and MathJax as Kramdown’s math engine. In result, inline math equation will be rendered into a script block with “math/tex” as type. Like this: <code class="language-plaintext highlighter-rouge">&lt;script type="math/tex"&gt; tex code &lt;/script&gt;</code>. Similarly the display style math equation will turn into a script block with “math/tex; mode=display” as type. Like this: <code class="language-plaintext highlighter-rouge">&lt;script type="math/tex; mode=display"&gt; tex code &lt;/script&gt;</code>.</p>

<noscript><pre>markdown: kramdown
kramdown:
  math_engine: mathjax</pre></noscript>
<script src="https://gist.github.com/xu-cheng/cb91d3284971250cb14e.js?file=_config.yml"> </script>

<p>Secondly, let us put the required resources into the HTML head.</p>

<noscript><pre>&lt;!-- Load jQuery --&gt;
&lt;script src=&quot;//code.jquery.com/jquery-1.11.1.min.js&quot;&gt;&lt;/script&gt;
&lt;!-- Load KaTeX --&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;//cdnjs.cloudflare.com/ajax/libs/KaTeX/0.1.1/katex.min.css&quot;&gt;
&lt;script src=&quot;//cdnjs.cloudflare.com/ajax/libs/KaTeX/0.1.1/katex.min.js&quot;&gt;&lt;/script&gt;</pre></noscript>
<script src="https://gist.github.com/xu-cheng/cb91d3284971250cb14e.js?file=head.html"> </script>

<p>Finally, with the power of jQuery and KaTeX, we replace all the TeX code block to rendered high quality equations fast. But remember, this JavaScript should be loaded at the end of HTML file.</p>

<noscript><pre>$(&quot;script[type=&#39;math/tex&#39;]&quot;).replaceWith(
  function(){
    var tex = $(this).text();
    return &quot;&lt;span class=\&quot;inline-equation\&quot;&gt;&quot; + 
           katex.renderToString(tex) +
           &quot;&lt;/span&gt;&quot;;
});

$(&quot;script[type=&#39;math/tex; mode=display&#39;]&quot;).replaceWith(
  function(){
    var tex = $(this).text();
    return &quot;&lt;div class=\&quot;equation\&quot;&gt;&quot; + 
           katex.renderToString(&quot;\\displaystyle &quot;+tex) +
           &quot;&lt;/div&gt;&quot;;
});
</pre></noscript>
<script src="https://gist.github.com/xu-cheng/cb91d3284971250cb14e.js?file=katex_render.js"> </script>

<p>Here’s the final result of rendered equation in below. Also you can do whatever custom you want in CSS file using class <code class="language-plaintext highlighter-rouge">inline-equation</code> and <code class="language-plaintext highlighter-rouge">equation</code>.</p>

<div class="kdmath">$$
P(x)=\frac{1}{\sigma\sqrt{2\pi}}e^{-\frac{(x-\mu)^2}{2\sigma ^2}}
$$</div>]]></content><author><name></name></author><category term="LaTeX" /><category term="Jekyll" /><summary type="html"><![CDATA[So as a Ph.D. student who studies in Computer Science, using LaTeX to render the equations in my blog is inevitable. In general, I hope the render can support the following features:]]></summary></entry><entry><title type="html">Hello World</title><link href="https://xuc.me/blog/hello-world/" rel="alternate" type="text/html" title="Hello World" /><published>2014-11-29T00:00:00+00:00</published><updated>2014-11-29T00:00:00+00:00</updated><id>https://xuc.me/blog/Hello-World</id><content type="html" xml:base="https://xuc.me/blog/hello-world/"><![CDATA[<p>This is my new blog and my personal academic web site.</p>]]></content><author><name></name></author><summary type="html"><![CDATA[This is my new blog and my personal academic web site.]]></summary></entry></feed>