Skip to content

Instantly share code, notes, and snippets.

@RuRo
Last active January 16, 2024 06:40
Show Gist options
  • Save RuRo/0ac5cf142c530575fb8cc98d1266c1fc to your computer and use it in GitHub Desktop.
Save RuRo/0ac5cf142c530575fb8cc98d1266c1fc to your computer and use it in GitHub Desktop.
Useful LaTeX presets for pretty printing code.

How to use code_presets.tex

  1. Create a new .tex file for your project.
  2. Copy %%-----DOCUMENT section into your file. Everything else will go into the $DOCUMENT CONTENTS HERE$ section.
  3. Select files, which you want to format. Currently supported formats are makefile, C code and README files.
    • Copy %%-----C, %%-----README or %%-----MAKEFILE section into your document.
    • Replace $FILE CONTENTS HERE$ with the contents of your file.
  4. Optional:
    • If you want your code sections to be labled with filenames, add a %%-----FILENAME section before the code section you are labeling. And replace $FILENAME HERE$ with full path to your file.
    • Split your document into pages by adding \newpage between code sections.
    • Split one file into pages by inserting %%-----PAGEBREAK section inside code sections. (It looks much better than the default paging, trust me.)
    • In the %%------README section, you can highlight words in bold with <:words in bold:> and italicize words with <|words in italics|>.
    • Add extra keywords (like size_t or FILE) for highlighting, by adding the %%-----MOREKW section before the code section and replacing $EXTRA KEYWORDS HERE$ with the keywords you want highlighted. (Comma seperated list. Sadly, no spaces are allowed in keywords)

Notes.

The paging is set up in such a way, that exactly 80 characters fit horizontally. If your listing looks ugly, because of hard wrapping lines/ugly alignment, please consider refactoring your code, so it does fit into 80 characters.

Installing TeX and the packages used in this preset, may be quite bothersome. As an alternative, I suggest using an online latex editor/compiler. Not all of them work. It is almost guaranteed that overleaf will work.

%%-----DOCUMENT
\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[a4paper, top=0.75cm, bottom=0.75cm, left=2cm, right=2cm]{geometry}
\usepackage{listings}
\usepackage{inconsolata}
\pagenumbering{gobble}
\lstset{frame=single, showstringspaces=false, columns=fixed, basicstyle={\ttfamily}, commentstyle={\it}, numbers=left, tabsize=4}
\begin{document}
$DOCUMENT CONTENTS HERE$
\end{document}
%%-----DOCUMENT
%%-----FILENAME
\lstset{title=Contents of \texttt{$FILENAME HERE$}}
%%-----FILENAME
%%-----PAGEBREAK
\end{lstlisting}
\newpage
\begin{lstlisting}[firstnumber=last]
%%-----PAGEBREAK
%%-----MOREKW
\lstset{morekeywords={$EXTRA KEYWORDS HERE$}}
%%-----MOREKW
%%-----MAKEFILE
\lstset{language={[gnu]make}}
\begin{lstlisting}
$FILE CONTENTS HERE$
\end{lstlisting}
%%-----MAKEFILE
%%-----README
\lstset{
language=,
moredelim=[is][\it]{<|}{|>},
moredelim=[is][\bf]{<:}{:>}
}
\begin{lstlisting}
$FILE CONTENTS HERE$
\end{lstlisting}
%%-----README
%%-----C
\lstset{language={[ANSI]C}}
\begin{lstlisting}
$FILE CONTENTS HERE$
\end{lstlisting}
%%-----C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment