Custom code highlighting in LaTeX

LaTeX is the software that I prefer for creating presentation slides or small documents with technical and mathematical background. From time to time it is necessary to show code with highlighting in LaTeX documents. Sometimes also for less popular programming languages or file formats.

LaTeX listings

A very popular code highlighting package for LaTeX is listings. This package is currently maintained by Prof. Dr. rer. nat. Jobst Hoffmann.

Minimal listings PHP example

Here a minimal listings example with some PHP code.

% php.tex
\documentclass{beamer}

\usepackage{color}
\usepackage{listings}

\lstset{
    columns=flexible,
    keepspaces=true,
    showstringspaces=false,
    commentstyle=\color{gray},
    keywordstyle=\color{purple},
    stringstyle=\color{green}
}

\begin{document}
    \begin{frame}[fragile]{listings PHP example}
        \begin{lstlisting}[language=PHP]
<?php
// Minimal PHP example
echo "Hello LaTeX!";
        \end{lstlisting}
    \end{frame}
\end{document}

The code from above will result in a beamer slide like the following one.

LaTeX listing PHP code highlighting

LaTeX listings Dockerfile code highlighting

At the moment is no code highlighting for Dockerfiles available. So let us build our own. The important part is the following one.

\lstdefinelanguage{Dockerfile}
{
  morekeywords={FROM, RUN, CMD, LABEL, MAINTAINER, EXPOSE, ENV, ADD, COPY,
    ENTRYPOINT, VOLUME, USER, WORKDIR, ARG, ONBUILD, STOPSIGNAL, HEALTHCHECK,
    SHELL},
  morecomment=[l]{\#},
  morestring=[b]"
}

Together with the coloring from above and this Maven Dockerfile we have a small LaTeX listings Dockerfile example.

% dockerfile.tex
\documentclass{beamer}

\usepackage{color}
\usepackage{listings}

\lstdefinelanguage{Dockerfile}
{
  morekeywords={FROM, RUN, CMD, LABEL, MAINTAINER, EXPOSE, ENV, ADD, COPY,
    ENTRYPOINT, VOLUME, USER, WORKDIR, ARG, ONBUILD, STOPSIGNAL, HEALTHCHECK,
    SHELL},
  morecomment=[l]{\#},
  morestring=[b]"
}

\lstset{
    columns=flexible,
    keepspaces=true,
    showstringspaces=false,
    basicstyle=\tiny\ttfamily,
    commentstyle=\color{gray},
    keywordstyle=\color{purple},
    stringstyle=\color{green}
}

\begin{document}
    \begin{frame}[fragile]{listings Dockerfile example}
        \begin{lstlisting}[language=Dockerfile]
# Example taken from https://github.com/carlossg/docker-maven
FROM openjdk:7-jdk-alpine

RUN apk add --no-cache curl tar bash

ARG MAVEN_VERSION=3.5.2
ARG USER_HOME_DIR="/root"
ARG SHA=707b1f6e390a65bde4af4cdaf2a24d45fc19a6ded00fff02e91626e3e42ceaff
ARG BASE_URL=https://apache.osuosl.org/maven/maven-3/${MAVEN_VERSION}/binaries

RUN mkdir -p /usr/share/maven /usr/share/maven/ref \
  && curl -fsSL -o /tmp/apache-maven.tar.gz ${BASE_URL}/apache-maven-${MAVEN_VERSION}-bin.tar.gz \
  && echo "${SHA}  /tmp/apache-maven.tar.gz" | sha256sum -c - \
  && tar -xzf /tmp/apache-maven.tar.gz -C /usr/share/maven --strip-components=1 \
  && rm -f /tmp/apache-maven.tar.gz \
  && ln -s /usr/share/maven/bin/mvn /usr/bin/mvn

ENV MAVEN_HOME /usr/share/maven
ENV MAVEN_CONFIG "$USER_HOME_DIR/.m2"

COPY mvn-entrypoint.sh /usr/local/bin/mvn-entrypoint.sh
COPY settings-docker.xml /usr/share/maven/ref/

VOLUME "$USER_HOME_DIR/.m2"

ENTRYPOINT ["/usr/local/bin/mvn-entrypoint.sh"]
CMD ["mvn"]
        \end{lstlisting}
    \end{frame}
\end{document}

The example from above has the following output. A pretty highlighted Dockerfile in a LaTeX beamer slide.

LaTeX listing Dockerfile code highlighting
Next Previous