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.

  • 2
    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. Mar 6, 2012 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?
    – Dean
    Mar 6, 2012 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:

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

It could then be used like this:

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

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}\\}

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:

enter image description here






\noindent The command:
\lstinline[style=BashInputStyle]´# apt-get --purge remove rubygems´.
removes the \Package{rubygems} package.

\noindent Consider the following command:
    # apt-get --purge remove rubygems
This removes the \Package{rubygems} package.
  • Similarly, there is the minted package for code syntax highlighting. It, however, relies on the installation of the python syntax highlighter Pygments.
    – cm2
    Mar 6, 2012 at 17:05
  • Peter, thanks for the advice, your style for highlighting bash is 10x prettier than the default settings! Oct 8, 2012 at 18:34
  • @AronAhmadia: Good to know that it was helpful. Oct 8, 2012 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. Oct 8, 2012 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. Oct 8, 2012 at 21:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .