[Tex/LaTex] How to make bash-code look good both in LaTeX and HTML

bashhtlatexhtmlverbatim

As I am new to Latex and this kind of typing in general, I find myself in need of some basic guidelines when it comes to how to make things look good in text. I have a document that looks good when typeset into a .pdf, but when I compile it to .html using htlatex the text loses its color, making it more difficult to distinguish from the surrounding text.

How can I write bash text so that it looks good when compiled into an html-file? Is there a commonly used method that makes bash look good?

Right now I use an environment every time:

\newenvironment{vtim}
{\color{magenta}
\begin{tt}
\fontsize{11pt}{11pt}\selectfont}
{\end{tt}}

And then, when I want bash in text:

\begin{vtim}
\$ Text text text text text
\end{vtim}

Best Answer

For pdf output you can use the minted package. This produces a nicely formatted and coloured PDF file for you.

As described in the minted package documentation, you have to install Pygments and run (pdf)latex with the -shell-escape option. To use this approach to produce html you also need to pass the -shell-scape option to htlatex. You can do this using

htlatex file.tex "" "" "" -shell-escape

Note that the three double quotes "" "" "" are necessary as the -shell-escape has to be the fifth option to htlatex.

Running htlatex on my MWE below gives the following html output:

HTML output

I was going to put up two images but, as far as I can tell, the PDF and HTML output are identical.

Finally, here is my MWE:

\documentclass{article}
\usepackage{minted}
\begin{document}
\begin{minted}{c}
#!/bin/sh
# removes backup files and TeX files

if [ $# -gt 0 ] && [ $1 = "-b" ]
then
    shift
    backups=false
else
    backups=true
fi

...
\end{minted}
\end{document}

Btw, I just learnt that minted also has a very useful command:

\inputminted[⟨options⟩]{⟨language⟩}{⟨filename⟩}

You can use this to typeset your source code without physically including it in your tex document.