# Convert shortcodes using Latex

Hi there,
I’m using some shortcodes to generate warning boxes in my doc:

{{%warning%}}
This is **very** important.
{{%/warning%}}


This works well to generate the website’s HTML:

However, I try to generate a LaTex file from the same markdown file:

pandoc -o mydoc.tex --template mytemplate.tex mydoc.md


I use pandoc with a latex template (mytemplate.tex) to render the markdown in latex.
This should allow me to create a PDF for my documentation.

However, the shortcodes are not rendered in the produced tex file:

\{\{\%warning\%\}\}
This is \textbf{very} important.
\{\{\%/warning\%\}\}


This doesn’t play well with the shortcode: they are not converted to LaTex.
I would like that the shortcodes are rendered as some LaTex environment:

\begin{warning}
This is \textbf{very} important.
\end{warning}


After that, it should be easy to define a new environment in my template for generating the warning box.
Do you know any way to do that?

Hi there,

Shortcodes are a Hugo-specific thing. Pandoc would not know what to do with them, so it would just be treated as text.

2 Likes

@pointyfar yes. What I do now:

1. generate the .tex from .md using pandoc
2. replace the shortcodes with environments in the .tex using sed command
3. run pdflatex to get the final PDF.

Works, although a bit hacky.

This is what I do (copying here for reference):

  cat index.md | pandoc -t latex --template template.tex | sed -f replace_shortcodes.sed > index.tex
pdflatex index.tex


This would produce a nice PDF from the .md files. I first extracted the latex template this way:

pandoc -D latex > template.tex


This gives me the default latex template used by pandoc. I can then customize it as I want.
Regarding shortcodes, I apply the following sed transformations:

s#\\{\\{\\%warning\\%\\}\\}#\\begin{warning}\n#
s#\\{\\{\\%/warning\\%\\}\\}#\n\\end{warning}#


This will replace each shortcode with an equivalent latex environment. For example, {{%warning%}} will be changed into \begin{warning}. The sed code looks awful, but that’s because there is a lot of escaping to do.

Finally, in template.tex I added the code for creating the info boxes:

% Colored boxes
{\def\FrameCommand{\colorbox{#1}}%
\MakeFramed {\FrameRestore}}%
{\endMakeFramed}

% info boxes.
% first param: color of the box
% second param: symbol to use for the icon
% minipage for the icon
\begin{minipage}[c][1cm][c]{1.5cm}
\begin{tikzpicture}[remember picture,overlay]
% create a colored cricle, with the symbol inside
\draw[#1, fill=#1!100] (0.4,0) circle (.4cm)
node  {\fontsize{8mm}{11mm}\usefont{T1}{put}{b}{b}\color{white}#2};
\end{tikzpicture}
\end{minipage}%
% minipage for the text
\begin{minipage}[t]{.9\textwidth - 1.5cm}}

\newenvironment{action}

\newenvironment{warning}

\newenvironment{note}


This will create nice info boxes in the pdf:

However, this method is not so robust (as it relies on sed replacements), and will not work on shortcode parameters, such as: {{%warning title="attention"%}}.
Do you know a better way?

Ok, now I sort of understand the question better.

Take this with the disclaimer that I don’t have a lot of experience with either LaTex or Pandoc, so YMMV.

Do you specifically need to use Pandoc, or is it more of a middleman to get from Hugo + markdown to LaTex? You could generate the html from Hugo and use that as input to generate tex.

Another avenue to explore would be Hugo’s custom output formats. The idea is to generate LaTex as a custom output format. But I don’t know how much work that would be, not having done that personally…

That wasn’t really very helpful, eh.

I hope whichever solution you end up with that you give an update, as I’m quite intrigued by this now. Best of luck.