# How can I add 1 line of source code (source code snippet) in a LaTeX document?

The easiest way of explaining this is probably to provide an example of what I want to do:

Consider the following section of a LaTeX document.

Calls to the function
// a blank line
get_next_random_number(int) // This should be monospace (texttt) and centered on page
// another blank line
should be independent of previous calls, resulting in a Fourier Space distribution with no eigenfrequency.


So far I have this (LaTeX code):

Calls to \\\\\texttt{get_next_random_number(int)}\\\\should be independent of previous calls, resulting in a Fourier Space distribution with no eigenfrequency.


But I have no idea how to set this so it is centred on the screen rather than left-justified.

• What you want to centre is not quite clear. The whole text? Only the text in monospaced font? – Bernard Aug 30 '15 at 14:29
• Only the monospaced bit - the line of source code. – user3728501 Aug 30 '15 at 14:29
• Well, why not use the center environment? – Bernard Aug 30 '15 at 14:30
• @Bernard is this: \begin{center}....\end{center}? – user3728501 Aug 30 '15 at 14:33
• You mean the centred thing is on its own line, but with no vertical spacing added? There is the \centerline{…} command. – Bernard Aug 30 '15 at 14:46

Do you looking something like this?

Edit: document classes as article, report,andbool among many others contain facility for writing verbatim text, i.e. a text as it written by typewriter. It has two forms:

\begin{verbatim}
text_which_will_appear_in_final_document_as_here
(with "_" between words, which otherwise require
to be written as "\_" ...
\end{verbatim}


which people usually use for presenting some code (as it is in your case) or in short form for presenting this code in a text by \verb{...} or \verb+...+ or any other pair of equal characters. Above image I obtain with the following MWE:

\documentclass[12pt]{article}

\begin{document}
Calls to
\begin{center}
\verb+get_next_random_number(int)+
\end{center}
should be independent of previous calls, resulting in a Fourier Space distribution with no eigenfrequency.
\end{document}


This form I select since you ask how to write code in typewriter characters centered in the text. If you for exaple like to have this code for example four characters form left text border, you can write as

Calls to
\begin{verbatim}
get_next_random_number(int)
\end{verbatim}
should be independent of previous calls, resulting in a Fourier Space distribution with no eigenfrequency.


For verbatim environments also exist specialized packages as fancyvrb and others, which enables more sophisticated writing of verbatim texts.

• What is \verb? Is this specifically for source code? I notice it puts the tt type font in there automatically. – user3728501 Aug 30 '15 at 15:16
• @user3728501, \verb/ is macro for typing a text as it is (shortens for verbatim). It is available in all basic LateX document class (article, report, book). For above example I use article. – Zarko Aug 30 '15 at 16:24
• @user3728501, I upgrade my answer with complete MWE as well with short explanation of verbatim environments. I hope that this will help you. – Zarko Aug 30 '15 at 20:43

Another option is use the fancyvrb package and the BVerbatim environment:

\documentclass{article}
\usepackage{fancyvrb}
\begin{document
Calls to the function
\begin{center}
\begin{BVerbatim}
get_next_random_number(int)
\end{BVerbatim}
\end{center}
should be independent of previous calls, resulting in a Fourier Space distribution with no eigenfrequency.
\end{document}


Calls to

• Don't leave blank lines around the center environment. – Sigur Aug 30 '15 at 20:43