## LaTeX forum ⇒ Text Formatting ⇒ Caption name issue using listings package Topic is solved

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Singularity
Posts: 155
Joined: Sat Jan 22, 2011 9:55 pm

### Caption name issue using listings package

The listings package won't load only some of my program files. The first file in the following MWE loads no problem, but the second one returns errors about inserting missing "$" and missing "}" and then about how it had extra or forgotten "$" and "}". Why doesn't it just not put in the extra "\$" and "}"?

The issue seems to be the difference in caption names and, more specifically, that the second caption has a "2D" in it (or maybe because it has an underscore in it), which must look like math to TeXnicCenter.

How do I get it to recognize this as text? How do I get this working?

Thanks.

\documentclass[]{article}\usepackage{listings}\begin{document} \lstdefinestyle{mycode}{basicstyle=\small, % print whole listing small	numbers=none,%left, numberstyle=\tiny, stepnumber=5, numbersep=5pt,	frame=single,	breaklines=true} \lstset{	style=mycode,	language=Octave,	caption={initFPU.m},	label={code:initFPU.m}}\lstinputlisting{code/kruskal/initFPU.m} \lstset{	style=mycode,	language=Octave,	caption={initFPU_2D.m},	label={code:initFPU_2D.m}}\lstinputlisting{code/kruskal/initFPU_2D.m}\end{document}

Note: you can't get this to work in writelatex without some files named initFPU.m and initFPU_2D.m.

Singularity
Posts: 155
Joined: Sat Jan 22, 2011 9:55 pm
It seems I can fix it by putting a "\" before the "_". But it prints as a kind of short "_". If there's a better fix, I'm still interested.

hugovdberg
Posts: 133
Joined: Sat Feb 25, 2012 6:12 pm
The underscore is indeed an active character that is only allowed in mathmode. So to print a literal _ you need \_, which simply prints the underscore from the current font. In the default font this is a rather narrow character, to make a wider underscore you could something like this:

\documentclass{article} % change the letter inside the [LaTeX]\hphantom[/LaTeX] to a wider or narrower character to change the width of the underscore\newcommand{\wideunderscore}{\underline{\hphantom{n}}} \begin{document}init\wideunderscore 2D init\_2D\end{document}
Ubuntu 13.10 + Tex Live 2013 + Texmaker / Windows 7 Pro + MikTex 2.9 + TexnicCenter / Android 4.3 + TexPortal + DroidEdit