3

I am new to Latex, I need to create a user defined command or Macro. I need to print unix shell commands on a pdf, the code looks like below

\begin{lstlisting}[language=bash]
$sudo apt-get install build-essential
\end{lstlisting}

This has to be done for every unix command and I have lot too many of them. For this reason I thought I can create a new command called \shell like this

\newcommand{\shell}[1]{
\begin{lstlisting}[language=bash]
#1
\end{lstlisting}
}

I am getting some !Emergency stop errors. can someone tell me what am I doing wrong. Thank you.

4

Try using this command and you will not receive that emergency stop error:

\newcommand{\shell}[1]{\lstinline[language=bash] {#1} } 

I am not sure about correctness of using newcommand while, in fact, you are defining newenvironment. If you are interested in defining a new environment, you can use the syntax addressed here: https://en.wikibooks.org/wiki/LaTeX/Macros#New_environments

  • 2
    Even better, \newcommand{\shell}{\lstinline[language=bash]} – egreg Feb 25 '16 at 18:37
4

As stated in comments, I would dissuade from trying to wrap lstlisting into custom environments. They are really fragile and do behave different from "normal" tex commands. But listings itself provides the ability to define own lstnewenvironment which saves you from typing [language=bash] every time.

\documentclass{article}

\usepackage{listings}

\lstnewenvironment{shell}{\lstset{language=bash}}{}

\begin{document}

\begin{shell}
$sudo apt-get install build-essential
\end{shell}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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