17

Possible Duplicate:
Syntax Coloring in LaTeX
Unix Command Highlighting latex

I am after some advice on how to display Linux terminal commands into LaTeX, so that they are indented and look different from normal text.

I have tried to insert the following into LaTeX

  $\emph {wget http://etc...................}

this outputs the wget onto a single line and then the part starting http onto another line. I want it all to appear on one line. Can someone body please give me some advice.

  • Could you give us more of your code? I'm getting the output as you seem to want it. Also, for command line text I'd used \ttfamily. Finally, you've got a stand alone $ which should either be \$ or not there at all. – A.Ellett Nov 25 '12 at 16:00
  • 3
    See the answer of of tex.stackexchange.com/questions/46953/… – TeXtnik Nov 25 '12 at 16:03
  • 1
    At a simple level remember LaTeX's standard command \verb and the environement verbatim. For more sophisticated formatting see also tex.stackexchange.com/q/867/15925 – Andrew Swann Nov 25 '12 at 16:39
  • 2
    Welcome to TeX.sx! Usually, we don't put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Upvoting is the preferred way here to say "thank you" to users who helped you. – Andrew Swann Nov 25 '12 at 16:44
32

You can use the features provided by the listings package.

\documentclass{article}

\usepackage{listings}

\begin{document}

\noindent See the following command :
\begin{lstlisting}[language=bash]
  $ wget http://tex.stackexchange.com
\end{lstlisting}

\end{document}

Listing

| improve this answer | |
  • What if my command is long and cuts off at the margin? – Vin Shahrdar Jun 6 '19 at 1:42
22

You can also use the minted package:

\documentclass{article}
\usepackage{minted}

\begin{document}
\noindent See the following command :
\begin{minted}{bash}
  $ wget http://tex.stackexchange.com
\end{minted}
\end{document}

Compiled result of above code

You need Pygments and the --shell-escape flag when you compile.

| improve this answer | |
5

You can use pythontex. It uses the Python library Pygments to provide some syntax highlighting.

If you don't need syntax highlighting, fancyvrb is nice for typesetting verbatim text with custom indentation, frames, line numbers, etc. Internally, pythontex uses much of fancyvrb, combined with syntax highlighting from Pygments.

enter image description here

\documentclass{article}

\usepackage{pythontex}
\setpygmentspygopt{bash}{style=default} %Set syntax highlighting style
\setpygmentsfv{xleftmargin=4ex} %Pass fancyvrb options, in this case, left margin

\begin{document}

\noindent Block use:
\begin{pygments}{bash}
$ wget http://tex.stackexchange.com
\end{pygments}
And after the block.

Inline use:  \pygment{bash}{wget http://tex.stackexchange.com}.  And after inline

\end{document}
| improve this answer | |
3

You can use the listings packages:

Add the following to the header:

\usepackage{listings}

And type your code like this:

\begin{lstlisting}
print('print me')
\end{lstlisting}

For more detail, read the Wikibooks chapter about the package. Wikibooks

| improve this answer | |
  • I like your answer :) I think you would have received more up votes if you had made a complete MWE, as the other answer did; Welcome to TeX.SE! – cmhughes Nov 25 '12 at 16:57

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