# Format Code/Pseudocode

In a document containing a lot of other stuff, I have the following code which I have kept in verbatim(for lack of other ideas). As you can see, the alignment doesn't look so good.
How do I make it more presentable, so that it becomes more readable, with indentation perhaps(verbatim doesnt seem to handle spaces and tabs)?

\documentclass[]{article}
\begin{document}
\begin{verbatim}

if (n == 0 || n == 1){
return n;
}
j = 0;
for (i = 0; i < n-1; i++){
if (arr[i] != arr[i+1]){
arr[j] = arr[i];
j++;
}
}
arr[j++] = arr[n-1];

\end{verbatim}
\end{document}

• A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "{}" on it). – DG' Feb 26 '19 at 12:01
• @DG' Is it more readable now? I wanna know how I could present it in a Latex Document, not on tex.stackexchange. – GermanShepherd Feb 26 '19 at 12:13
• Do you want LaTeX to indent it for you? It isn't the right tool for the job. There are editors out there that will support the programming language and will have auto-indentation for it, so why not using one of those? – TeXnician Feb 26 '19 at 12:13
• Well what I see in your screenshot is some verbatim code which you claim that one would see to be badly aligned. I do not get which kind of more readability you want. Making it stand out from ordinary text (maybe listings would help here) or making it nice in itself (external code formatting might be the way to go). Also your claim that verbatim can't handle spaces or tabs is wrong. – TeXnician Feb 26 '19 at 12:23
• For the automatic indenting you need to call an external tool, as neither listings or minted provides such functionality as far as I know. This can be done from within LaTeX with a (medium) bit of trouble. Which operating system do you use? – Marijn Feb 26 '19 at 12:51

Something like this?

\documentclass{book}
\usepackage{listings}

\lstdefinestyle{myListingStyle}
{
basicstyle = \small\ttfamily,
breaklines = true,
}

\begin{document}

\begin{lstlisting}[
style = myListingStyle,
caption = {Nice listing.}
]
if (n == 0 || n == 1){
return n;
}
j = 0;
for (i = 0; i < n-1; i++){
if (arr[i] != arr[i+1]){
arr[j] = arr[i];
j++;
}
}
arr[j++] = arr[n-1];
\end{lstlisting}

\end{document}


See Put a grey background behind code extracts in a Latex document (like this site does) for example for adding a background color. It's also possible to load the code from an external file -- just have a look at related questions.

• Is it possible to remove the caption for listings? – GermanShepherd Feb 26 '19 at 12:35
• @GermanShepherd Yes, just have a look at the code and remove caption = {Nice listing.}. – Dr. Manuel Kuehner Feb 26 '19 at 12:36

## Verbatim

Contrary to what you claim in your question, verbatim supports spaces and indentation, so why not use it?

\documentclass[]{article}
\begin{document}
\begin{verbatim}
if (n == 0 || n == 1){
return n;
}
j = 0;
for (i = 0; i < n-1; i++){
if (arr[i] != arr[i+1]){
arr[j] = arr[i];
j++;
}
}
arr[j++] = arr[n-1];
\end{verbatim}
\end{document}


## Listings

The listings package can add colors.

\documentclass[]{article}
\usepackage{listings}
\usepackage{xcolor}
\lstset{basicstyle=\ttfamily,language=c,keywordstyle=\color{blue}}
\begin{document}
\begin{lstlisting}
if (n == 0 || n == 1){
return n;
}
j = 0;
for (i = 0; i < n-1; i++){
if (arr[i] != arr[i+1]){
arr[j] = arr[i];
j++;
}
}
arr[j++] = arr[n-1];
\end{lstlisting}
\end{document}


## Minted

The minted package is even more powerful…

% arara: pdflatex: {shell: yes}
\documentclass[]{article}
\usepackage{minted}
\begin{document}
\begin{minted}{c}
if (n == 0 || n == 1){
return n;
}
j = 0;
for (i = 0; i < n-1; i++){
if (arr[i] != arr[i+1]){
arr[j] = arr[i];
j++;
}
}
arr[j++] = arr[n-1];
\end{minted}
\end{document}


## Tcblisting

You can of course combine it with tcolorbox or something else…

% arara: pdflatex: {shell: yes}
\documentclass[]{article}
\usepackage{minted}
\usepackage[minted]{tcolorbox}
\newtcblisting{mylisting}{listing only,listing engine=minted, minted language=c,colback=gray!20}
\begin{document}
\begin{mylisting}
if (n == 0 || n == 1){
return n;
}
j = 0;
for (i = 0; i < n-1; i++){
if (arr[i] != arr[i+1]){
arr[j] = arr[i];
j++;
}
}
arr[j++] = arr[n-1];
\end{mylisting}
\end{document}

• +1: Very complete answer :) – Dr. Manuel Kuehner Feb 26 '19 at 12:36
• @Dr.ManuelKuehner By far not, but listing some common options :) – TeXnician Feb 26 '19 at 12:37

As already stated by others, verbatim does work for code (I use it for that purpose). The issue you see is that you usually format your code with tabs which verbatim is ignoring for some reason. So what I do is I put my code into verbatim select it and replace tabs with say 4 (or any number for that matter) of spaces (but just inside the selection).

• Are you sure that verbatim does not support tabs? In my case, it does. – user156344 Feb 26 '19 at 16:16
• i wrote a 80page script About the use of a finite element Analysis program that included a LOT of example Code with tabs. and i had to replace them all because else my Output would look like the one that the OP shared. this may depend on the tex Distribution however. – der bender Feb 26 '19 at 16:18