4

I know the question has been asked several times, but I was trying to specify some text in bold in my lstlisting environment.

While this was working perfectly with %\lstset{morecomment=[s][\bfseries]{@@}{@@}}, I was trying to make the listings changes local to that environment only. According to the listings package documentation, optional arguments to lstlistings must be passed via square brackets. But as my command has already square brackets in it, I need to surround these brackets by curly brackets.

Despite I'm correctly following the recommendations, the following small working example doesn't compile and crashes with this error File ended while scanning use of \lst@Delim@delim.

\documentclass{report}
\usepackage{listings}
\begin{document}    
\begin{lstlisting}[{morecomment=[s][keywordstyle]{@@}{@@}}] 
Some test loru
@@ hello world @@
lorem ipsum
\end{lstlisting}
\end{document}

Also (and maybe this would require me to ask a dedicated question for that), the two characters @@ are displayed in the rendered version of the text. I don't want these two characters to appear.

Finally, the rendered bold version, here after with some Objective-C code, isn't bold enough for me. I already tried several tricks with some additional packages, but I'm not satisfied of the results. Do I need to change the font? If so, what monospace font do you recommend me instead of the default one I'm using?

enter image description here

  • I can expand this into an answer if you need more detail, but with respect to the bold not being bold enough, if you use latin modern (almost identical to the default computer modern, but better in many cases) \ttfamily\fontseries{dk}\selectfont should give you "Latin Modern Typewriter Dark" which contrasts well (at the same width) with \ttfamily\fontseries{lt}\selectfont, the "Light" variant. Details (pdf). I had the option of using colour highlighting and used the rather nice condensed variant (no bold). – Chris H Mar 31 '15 at 18:54
  • For those who are still looking for the bold font not bold enough, please read [tex.stackexchange.com/questions/33039/… answer). – wget Oct 18 '15 at 19:27
6

Joseph has already told you how to make the change locally.

However, here are some more hints.

You need

moredelim={[is][keywordstyle]{@@}{@@}}

and not

morecomment={[s][keywordstyle]{@@}{@@}}

to achieve what you want.

MWE:

\documentclass{report}
\usepackage{listings}
\begin{document}
\begin{lstlisting}[moredelim={[is][keywordstyle]{@@}{@@}}]
Some test loru
@@ hello world @@
lorem ipsum
\end{lstlisting}
\end{document} 

enter image description here

5

You need braces just around the argument part

\documentclass{report}
\usepackage{listings}
\begin{document}    
\begin{lstlisting}[morecomment={[s][keywordstyle]{@@}{@@}}]
Some test loru
@@ hello world @@
lorem ipsum
\end{lstlisting}
\end{document}

This works by 'hiding' the inner [...] pairs from the parser: needed as the LaTeX2e mechanisms do not do bracket-matching for these so the first ] will close the optional argument.

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.