Skip to content

Instantly share code, notes, and snippets.

@drorata
Created December 27, 2022 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drorata/b0b969e49f4bce91f67cf34efa6acf73 to your computer and use it in GitHub Desktop.
Save drorata/b0b969e49f4bce91f67cf34efa6acf73 to your computer and use it in GitHub Desktop.
MWE - rendering Markdown using a LaTeX template

I am using the function render_latex:

def render_latex(text: str, minimal: bool = True) -> str:
    logger.debug(f"Rendering LaTeX - {text}")
    with LaTeXRenderer() as rendered:
        ast = Document(text)
        if len(ast.children) != 1:
            raise ValueError("Number of children of provided text is not 1")
        for child in ast.children[0].children:
            if not isinstance(child, SUPPORTED_TOKENS):
                raise ValueError(f"Child type {type(child)} is not supported")
        res: str = rendered.render(ast)
    if not minimal:
        return res
    BEGIN_DOC = "\\begin{document}"
    END_DOC = "\\end{document}"
    return res[res.find(BEGIN_DOC) + len(BEGIN_DOC) : res.find(END_DOC)].strip("\n")

This function gets a Markdown string and returns a LaTeX snippet. The input is being read from a JSON (e.g. this one). Next, each LaTeX rendered element in the JSON is then injected into a template (e.g. here).

Specifically, consider the minimal JSON:

{
   "bio_data": {
       "firstname": "Ducky",
       "lastname": "Duck",
       "linkedin_id": "d_duck",
       "github_id": "git_duck",
       "website": "duck-at-lake.com",
       "profile_filename": "profile-pic",
       "email": "dduck@lake.com",
       "phone": "+999 (321) 321 654 987",
       "stars": [
           "list",
           "of",
           "keywords"
       ]
   },
   "skills": [
       {
           "type": "Type/category fo the skill",
           "items": [
               "Items",
               "Related",
               "to",
               "Skill",
               "see example",
               "Special chars like & can be used as well",
               "what about `inline code`?"
           ]
       },
       {
           "type": "Programming languages",
           "items": [
               "Python",
               "Java",
               "R",
               "bash"
           ]
       }
   ]
}

and the template:

\documentclass[11pt,a4paper,colorlinks,linkcolor=green]{moderncv}

\moderncvtheme[blue]{classic}
\definecolor{color2}{rgb}{0.,0.,0.}% dark grey
\usepackage[utf8]{inputenc}
\usepackage[scale=0.8]{geometry}
\usepackage{xstring}
\renewcommand*{\namefont}{\Huge\scshape}

% see: https://tex.stackexchange.com/q/30720/412
\newcommand\blfootnote[1]{%
  \begingroup
  \renewcommand\thefootnote{}\footnote{#1}%
  \addtocounter{footnote}{-1}%
  \endgroup
}

\firstname{\VAR{ bio_data["firstname"] }}
\familyname{\mbox{\VAR{ bio_data["lastname"] }}}
\title{%
    \small %
    \BLOCK{ for star_item in bio_data["stars"] }
    \VAR{star_item}\BLOCK{ if not loop.last} $\star$\BLOCK{ endif } %
    \BLOCK{ endfor }%
}%
\phone{\VAR{ bio_data["phone"] }}
\email{\VAR{ bio_data["email"] }}
\social[linkedin]{\VAR{ bio_data["linkedin_id"] }}
\social[github]{\VAR{ bio_data["github_id"] }}
\homepage{\VAR{ bio_data["website"] }}
\photo{\VAR{ bio_data["profile_filename"] }}

\definecolor{see}{rgb}{0.5,0.5,0.5}% for web links
\newcommand{\up}[1]{\ensuremath{^\textrm{\scriptsize#1}}}% for text subscripts
\renewcommand{\labelitemi}{$\circ$}


\begin{document}
\definecolor{links}{gray}{0.1}
\hypersetup{urlcolor=links}
\maketitle

\vspace*{-1cm}

\section{\textsc{Technical Skills}}
  \BLOCK{ for skill_cat in skills }
  \cvitem{\textbullet}{\textbf{\VAR{skill_cat["type"]}:}
    \BLOCK{ for item in skill_cat["items"] }
      \VAR{item}\BLOCK{ if not loop.last }, \BLOCK{ endif}\BLOCK{ endfor }}
    \BLOCK{ endfor }

\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment