4

I have to add in a Latex document a page with the code from program I use for the report. However, when I simply copy and paste the code into Latex, I get a ton of warnings. Is there a way to tell Latex to ignore all of the special characters in the code, just in this part of the report?

  • 1
    Packages like listings or minted take care of that/ – Johannes_B Jul 24 '16 at 12:02
2

One option is to use the verbatim environment:

\begin{verbatim}
The verbatim environment
  simply reproduces every
 character you input,
including all  s p a c e s!
\end{verbatim}

Sample taken from here: https://en.wikibooks.org/wiki/LaTeX/Paragraph_Formatting#Verbatim_text

Similar environment with line breaking and a tiny font size using listings package:

\begin{lstlisting}[breaklines,basicstyle=\tiny]
This is a very long line that breaks and is tiny...
\end{lstlisting}

And like Johannes_B wrote, there are dedicated packages for displaying source code listings:

https://en.wikibooks.org/wiki/LaTeX/Source_Code_Listings https://www.sharelatex.com/learn/Code_Highlighting_with_minted

They allow line nubering, syntax coloring etc. Example using minted package:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

\usepackage{minted}

\begin{document}
\begin{minted}[frame=lines,linenos]{python}
def function():
    return
\end{minted}
\end{document}
  • The verbatim command works nice, but some parts of the text are not displayed, since the lines are sometimes too wide. – blackarrow Jul 24 '16 at 12:41
  • Ok. There are ways to fix that: break lines or change font size to smaller. I added an example to both break the lines and decrease the font size using lstlistingenvironment. – Paapaa Jul 24 '16 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.