3

Is it possible to display bash code in LaTeX? Like the next one:

Here is the code's format i want to show in latex

1 Answer 1

14

In LaTeX, in order to display bash code and any other code, you can use the listings package.

The \lstset command is used to control the basic appearance, including the language of code. Please see page 13 of listings documentation.

The code to be displayed may be part of the LaTeX file or may be an external file.

\documentclass{report}

\usepackage{listings}
\lstset{
  language=bash,
  basicstyle=\ttfamily
}

\begin{document}

Code directly embedded in \LaTeX\ file.

\begin{lstlisting}
  Here is the code's format i want to show in latex
\end{lstlisting}

Code from an external file.

\lstinputlisting{my.bash}

\end{document}

Other possible/related options for tasks like the above are (not in any particular order):

5
  • Thank you, I hadn't found an easy answer to this. Should it works for reportclass?
    – user134500
    Commented Aug 24, 2017 at 4:41
  • 3
    @santimirandarp Yes, why not? Updated my answer for report class. Now you can accept my answer and upvote it. :-)
    – Masroor
    Commented Aug 24, 2017 at 4:46
  • Yes, i will try it and accept and upvote if it works...thank you again.
    – user134500
    Commented Aug 24, 2017 at 4:51
  • you might add verbatim to that list. it doesn't have as many bells and whistles, but is simple, reliable, and maintained by the latex team so when something sneaky changes under the covers, it doesn't fall apart. Commented Aug 24, 2017 at 16:56
  • @barbarabeeton Yes, of course. How could I miss that?
    – Masroor
    Commented Aug 24, 2017 at 16:58

You must log in to answer this question.