Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
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. –  Roelof Spijker Mar 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? –  Dean Mar 6 '12 at 10:51

2 Answers 2

up vote 6 down vote accepted

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}\\}
share|improve this answer

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

Notes:

Code:

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}

\lstdefinestyle{BashInputStyle}{
  language=bash,
  basicstyle=\small\sffamily,
  numbers=left,
  numberstyle=\tiny,
  numbersep=3pt,
  frame=tb,
  columns=fullflexible,
  backgroundcolor=\color{yellow!20},
  linewidth=0.9\linewidth,
  xleftmargin=0.1\linewidth
}

\newcommand*{\Package}[1]{\texttt{#1}}%

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

\bigskip
\noindent Consider the following command:
\begin{lstlisting}[style=BashInputStyle]
    # apt-get --purge remove rubygems
\end{lstlisting}
This removes the \Package{rubygems} package.
\end{document}
share|improve this answer
    
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 '12 at 17:05
    
Peter, thanks for the advice, your style for highlighting bash is 10x prettier than the default settings! –  Aron Ahmadia Oct 8 '12 at 18:34
    
@AronAhmadia: Good to know that it was helpful. –  Peter Grill Oct 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 Ahmadia Oct 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 Grill Oct 8 '12 at 21:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.