5

I am recently introduced to tcolorbox, which is a great package. I am trying to use it to highlight computer codes such as bash, python, and c++ in my document. When I run the following MWE, I get the error: Illegal parameter number in definition of \next #. In the following MWE, when I remove the first comment line: # Unzip the file to the created directory it works with no problem. However, I would like to have the comment line at the beginning of the bash code. Could someone shed light on this?

\documentclass[11pt,letterpaper]{article}

\usepackage{tcolorbox}
\tcbuselibrary{minted,skins}

\newtcblisting{bashcode}[1][]{
  listing engine=minted,
  colback=bg,
  colframe=black!70,
  listing only,
  minted style=colorful,
  minted language=bash,
  minted options={linenos=true,numbersep=3mm,texcl=true,#1},
  left=5mm,enhanced,
  overlay={\begin{tcbclipinterior}\fill[black!25] (frame.south west)
            rectangle ([xshift=5mm]frame.north west);\end{tcbclipinterior}}
}
\definecolor{bg}{rgb}{0.85,0.85,0.85}

\begin{document}

\begin{bashcode}
# Unzip the file to the created directory
$ unzip code-163f67cbf550560aa351b3d0a3bbbd7a22863cb4.zip -d ~

$ mkdir ~/code

# Since the directory name is long, I do the following
$ mv ~/code-163f67cbf550560aa351b3d0a3bbbd7a22863cb4/ ~/code
$ cd ~/code
$ ls -F
apps/     inverters/           Makefile*  numerics/    sphfunc/
imaging/  logging.properties*  models/    simulation/

$ make
\end{bashcode}

\end{document}
4
  • Related: tex.stackexchange.com/questions/89276/…
    – user13907
    Apr 28, 2014 at 19:07
  • If you use \begin{bashcode}[] then everything works as expected
    – cmhughes
    Apr 28, 2014 at 19:08
  • @ cmhughes: Thanks. I added [] to the \begin{bashcode} and it worked flawlessly. What does these brackets do?
    – afp_2008
    Apr 28, 2014 at 19:10
  • 1
    @Ahm you've defined an environment that takes an optional argument that is empty by default. In most cases, leaving it blank by not writing it will be absolutely fine - you just happened to pick one of the characters that will cause you issues; remember that # has special meaning in LaTeX, and because your environment is scanning for an optional argument, # still has its primitive meaning, and as such, is not interpreted as verbatim text. using the [] tells LaTeX explicitly that there is no argument :)
    – cmhughes
    Apr 28, 2014 at 19:13

1 Answer 1

5

If you use

\begin{bashcode}[]

then your example compiles just fine (using shell-escape).

To explain this, remember that # is a special character in LaTeX. You have defined an environment that takes an optional argument that is empty by default. In most cases, leaving it blank by not writing it will be absolutely fine - you just happened to pick one of the characters that will cause you issues.

Because your environment is scanning for an optional argument, # still has its primitive meaning, and as such, is not interpreted as verbatim text. Using the [] tells LaTeX explicitly that there is no argument.

For further exploration, try replacing your very first # with any other character (modulo a guru finding another special case), and you'll notice that you don't have any issue.

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.