Usually \texttt is used for this kind of thing. You could wrap it with the \indent's in a command to make it semantically more pleasing. Something like \newcommand{\shellcmd}[1]{\indent\indent\texttt{\scriptsize\# #1}}. You could then simply use \shellcmd{apt-get --purge remove rubygems} where you want to display the command.
–
Roelof SpijkerMar 6 '12 at 10:22

@RoelofSpijker That looks a lot better, maybe you could write a bit more of an explanation and write it as an answer?
–
DeanMar 6 '12 at 10:51

2 Answers
2

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:

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:

I would recommend you use the listings package for this which allow for formatting of inline code snippets via a verbatim like macro, \lstinline, and also display style code for longer pieces of code via the lstlisting environment. Here is an example of the two styles:

Notes:

I would also recommend defining your own commands for emphasizing various portions of the text as I have done below for a \Package. This keeps your LaTeX code more meaningful and also allows simplifies an changes you may want to do later.

@AronAhmadia: Good to know that it was helpful.
–
Peter GrillOct 8 '12 at 18:41

I just rebuilt a PDF that contained many sample bash shell code listings, and I wanted to point out one small problem with your answer (on OS X). The \sffamily of fonts, although a bit prettier, does not copy-paste with the same correctness as \ttfamily by default for me. I switched to \ttfamily to get copy-paste working. Do let me know if there is a better fix/workaround.
–
Aron AhmadiaOct 8 '12 at 20:03

@AronAhmadia: The copy-paste issue is unrelated to highlighting, so would recommend you post a separate question. You can link back to this one if you want. That way others that have the same issue are more likely to be able to locate it should they encounter a similar issue.
–
Peter GrillOct 8 '12 at 21:06