[Tex/LaTex] Inserting LaTeX code into LaTeX

verbatim

I have a problem with adding text into a LaTeX document. I have a LaTeX document about LaTeX and I need to insert some code snippets. I need to add "\usepackage{musixtex}" as a text, which should be displayed as a text. Do you know how?

Best Answer

You can use \verb like this:

\documentclass{letter}
\begin{document}
Example: \verb!\usepackage{musixtex}!
\end{document}

Edits by Speravir and Sam Whited:

There is also a starred version, which makes the spaces visible:

\documentclass{article}
\begin{document}
\verb|\LaTeX vs. \LaTeX\ or \LaTeX{} \dots|

\verb*|\LaTeX vs. \LaTeX\ or \LaTeX{} \dots|

\LaTeX vs. \LaTeX\ or \LaTeX{} \dots
\end{document}

Multiline

If you end up with more than a short, one line, phrase you might also try the verbatim (built in) or Verbatim (an improved version located in the fancyvrb package) environments.

eg.

\begin{verbatim}
  Some LaTeX code here
  Which can be multiple lines
  \usepackage{musixtex}
  \begin{music}\nostartrule
  \instrumentnumber{1}

  ...
\end{verbatim}