Text FormattingCaption name issue using listings package

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

Caption name issue using listings package

Post by Singularity »

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.

Code: Select all

\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.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Singularity
Posts: 156
Joined: Sat Jan 22, 2011 9:55 pm

Re: Caption name issue using listings package

Post by Singularity »

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

Caption name issue using listings package

Post by hugovdberg »

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:

Code: Select all

\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
Post Reply