[Tex/LaTex] Unix Command Highlighting latex

highlighting

Is there any what I can easily show commands in latex? I am currently using:

\indent\indent\footnotesize{\# apt-get --purge remove rubygems} \\

But this isn't showing a very good disctintion between commands and text. What else could I do to show it a little more.

Best Answer

Normally a typewriter face is used to format shell commands. Just using \texttt everywhere is not very pleasing semantically though. Furthermore, if you ever want to change how shell commands are typeset you would have to go through your code and check every occurence of \texttt, determine if it is used to typeset a shell command and then possibly change it. Better would be to define a new command for this specific purpose. It makes it easy to see what you are typesetting and it allows you to change your mind about the typesetting quite easily at a later time. To define a new command we can use the LaTeX command \newcommand{cmd}[num of args]{definition}. This approach also allows us to wrap the extra indentation and newlines inside of the command. It would look like this:

\newcommand{\shellcmd}[1]{\\\indent\indent\texttt{\footnotesize\# #1}\\}

It could then be used like this:

\documentclass{article}
\newcommand{\shellcmd}[1]{\\\indent\indent\texttt{\footnotesize\# #1}\\}
\begin{document}
  \noindent Consider the following command:
  \shellcmd{apt-get --purge remove rubygems}
  This removes the \texttt{rubygems} package.
\end{document}

To the following effect:

typeset shell command

You should note the initial newline in the command. If you want to use it at the start of the line, you could define a starred version that doesn't have the initial newline. For this the suffix pacakge is best suited. You can then define a starred version without the initial newline as follows:

\WithSuffix\def\shellcmd*#1{\indent\indent\texttt{\footnotesize\# #1}\\}