Search code examples
rr-markdownmathjaxtinytex

How to make curly k in R markdown equations?


In R markdown equations, I need to make a curly lower case k, analogous to the curly lower case "l" generated by \ell. There are a few posts dealing with this, but very LaTex based. I can't find any posts on this issue with a solution for R markdown users who are unfamiliar with the nitty gritty of LaTex. I am using tinytex.


Solution

  • There are numerous ways to achieve this.

    PDF/LaTeX-only

    If you need PDF output only you may use a LaTeX package that supports lowercase calligraphic letters in math mode, e.g. dutchcal.

    Since this is not tex.stackexchange and you mention that you're not too familiar with Tex, I'm not going too much into detail. Here's a simple example of a PDF rmarkdown project:

    1. Set up a file preamble.tex:

      \DeclareMathAlphabet{\mathdutchcal}{U}{dutchcal}{m}{n}

      This ensures that dutchcal letters are used in math mode using \mathdutchcal{<letters>}.

    2. Ensure that the content of preamble.tex is included in the tex file from which the PDF will be generated by setting up the yaml header of your .rmd file accordingly:

      ---
      title: "Mathdutchcal"
      output: 
        pdf_document:
          include:
            in_header: preamble.tex
      ---
      
    3. Type lowercase curly letters in math mode:

      $$\mathdutchcal{hijkl}$$

    4. Knit to PDF. Result:

    As suggested by Ben Bolker, it is convenient to define macros in preamble.tex if you need to type certain curly letters frequently, e.g. with

    \newcommand{\curlyk}{\ensuremath\mathdutchcal{k}}
    

    you to get a curly k using \curlyk.


    PDF/LaTeX + HTML/MathJax

    As mentioned by Ben Bolker, the above will not work with HTML output because these commands/libraries are not supported by MathJax. Lowercase calligraphic letters in MathJax can be set with \mathscr. This should also work for PDF output if you compile using xelatex and define a suitable font, e.g. STIXTwoMath in preamble.tex.

    1. Set up preamble.tex:

      \setmathfont{STIXTwoMath-Regular.otf}

    2. Set up the yaml header:

      ---
      title: "PDF/LaTeX + HTML/MathJax"
      output: 
        pdf_document:
          latex_engine: xelatex
          include:
            in_header: preamble.tex
      ---
      
    3. Type lowercase curly letters in math mode:

      $$\mathscr{hijkl}$$

    4. Knit to PDF or HTML. Results (PDF top, HTML bottom):



    Remark. LaTeX packages (dutchcal and stix2-otf) needed for PDF output may not be not installed if you're using tinytex. tinytex should install them when you knit to PDF. If this fails you may install the package manually using a convenience function for the TeX Live Manager, e.g. tinytex::tlmgr_install("dutchcal").