-
-
Notifications
You must be signed in to change notification settings - Fork 133
code listings
There are multiple ways to insert code listings
This is the LaTeX native method for inserting code listings. It typesets the contents as is using a typewriter-like font. But dones not provide any type of fontification.
\begin{verbatim}
#include <stdio.h>
int main() {
printf ("hello world\n");
}
\end{verbatim}
The package “verbatim” extended the native verbatim evironemnt with some new features, including the command \verbatiminput{finalename}
that icludes the contents of a file as a verbatim environment.
The listings package ®memebr to ) creates its own evironment for code listings. This new environment is very customizable and reading the documentation is strongly advised. Still… if you are lazy, here are some suggestions:
\usepackage{listings} % Fontification of source code listings
\lstset{
captionpos=t,
basicstyle={\ttfamily\footnotesize},
numbers=left,
numberstyle={\ttfamily\tiny},
tabsize=2,
language=Java,
float,
frame=single,
columns=fullflexible,
breaklines=true,
postbreak=\mbox{\textcolor{red}{$\hookrightarrow$}\space},
inputencoding=utf8,
extendedchars=true,
literate=
{á}{{\'a}}1 {é}{{\'e}}1 {í}{{\'i}}1 {ó}{{\'o}}1 {ú}{{\'u}}1
{Á}{{\'A}}1 {É}{{\'E}}1 {Í}{{\'I}}1 {Ó}{{\'O}}1 {Ú}{{\'U}}1
{à}{{\`a}}1 {è}{{\`e}}1 {ì}{{\`i}}1 {ò}{{\`o}}1 {ù}{{\`u}}1
{À}{{\`A}}1 {È}{{\'E}}1 {Ì}{{\`I}}1 {Ò}{{\`O}}1 {Ù}{{\`U}}1
{ä}{{\"a}}1 {ë}{{\"e}}1 {ï}{{\"i}}1 {ö}{{\"o}}1 {ü}{{\"u}}1
{Ä}{{\"A}}1 {Ë}{{\"E}}1 {Ï}{{\"I}}1 {Ö}{{\"O}}1 {Ü}{{\"U}}1
{â}{{\^a}}1 {ê}{{\^e}}1 {î}{{\^i}}1 {ô}{{\^o}}1 {û}{{\^u}}1
{Â}{{\^A}}1 {Ê}{{\^E}}1 {Î}{{\^I}}1 {Ô}{{\^O}}1 {Û}{{\^U}}1
{œ}{{\oe}}1 {Œ}{{\OE}}1 {æ}{{\ae}}1 {Æ}{{\AE}}1 {ß}{{\ss}}1
{ç}{{\c c}}1 {Ç}{{\c C}}1 {ø}{{\o}}1 {å}{{\r a}}1 {Å}{{\r A}}1
{€}{{\EUR}}1 {£}{{\pounds}}1
}
...
\begin{lstlisting}[language=C]
#include <stdio.h>
int main() {
printf ("hello world\n");
}
\end{lstlisting}
The “minted” package uses Pygments for highlighting the code which supports just about any language you can think of. There is a good tutorial for the use of minted in the Overleaf docuemntation pages.
\usepackage{minted}
...
\begin{minted}{C}
#include <stdio.h>
int main() {
printf ("hello world\n");
}
\end{minted}
Thanks for using this template and for contributing with your suggestions. We really appreciate it. If you may, please don't send us emails directly. Post your question in the GitHub Discussions page or the Facebook Group and you will get your answer there — perhaps even faster.