# Inline listings crash when argument ends in “Error” under the Python language

Well, this is honestly the strangest thing I've ever seen concerning LaTeX.

A friend of mine encountered what I can only describe as a bug in listings; can it be resolved?

See the minimal example below with errors.

\documentclass{article}

\usepackage{listings}

\lstMakeShortInline[language=Python]"

\begin{document}
"ThisIsAFakeError".
"ThisIsAFakeErro".
\end{document}


## Error message:

ERROR: Missing } inserted.

--- TeX said ---
<inserted text>
}
l.8 "ThisIsAFakeError".

--- HELP ---
TeX has become confused. The position indicated by the error locator
is probably beyond the point where the incorrect input is.

ERROR: Undefined control sequence.

--- TeX said ---
\lst@eaten ->".

l.8 "ThisIsAFakeError".

--- HELP ---
TeX encountered an unknown command name. You probably misspelled the
name. If this message occurs when a LaTeX command is being processed,
the command is probably in the wrong place---for example, the error
can be produced by an \item command that's not inside a list-making
environment. The error can also be caused by a missing \documentclass
command.

-
"Er" is bad as well – David Carlisle Apr 10 '14 at 16:47
This probably has to do with the Python language definition, which uses " as string delimiter. Tell your friend that the choice of " as a shorthand for inline Python code is a bad one; a backtick, for instance, would be preferable. – Jubobs Apr 10 '14 at 16:54
@Jubobs The poor choice lies with me, unfortunately. A backtick makes much more sense, especially within the context of other plain-text markup languages like Markdown. I'll have to remember that for next time. :) – Sean Allred Apr 10 '14 at 16:57
I'm sure we could find something stranger than this:-) – David Carlisle Apr 10 '14 at 16:58

Comparing "Ej" (which works) with "Er" (which doesn't) \tracingall shows it goes wrong here

\lst@IfNextChars@@ #1#2\relax #3->\def \lst@tofind {#2}\lst@lAddTo \lst@eaten {
#3}\ifx #1#3\ifx \lst@tofind \@empty \let \lst@next \@tempa \else \let \lst@nex
t \lst@IfNextChars@ \fi \expandafter \lst@next \else \expandafter \@tempb \fi
#1<-"
#2<-""
#3<-"


where " is being used as a delimiting token and as the token being tested...

A workaround is to use something else eg ^

\documentclass{article}

\usepackage{listings}

\lstMakeShortInline[language=Python]^

\begin{document}
^ThisIsAFakeError^.
^ThisIsAFakeErro^.

\end{document}

-
Thanks for finding the issue, but can you explain what exactly is tripping up if you get the time? My TeX-fu yet leaves much to be desired… – Sean Allred Apr 10 '14 at 16:59
@SeanAllred Possibly, but first drive home, eat etc. Meanwhile you can read w3.org/TR/MathML3 (Hot off the press today) – David Carlisle Apr 10 '14 at 17:01

If "your friend" (yeah right! :p) looks up how the Python language is defined by listings in the file lstdvrs.dtx, "s/he" will see that the " character is defined as a string delimiter for that language:

morestring=[b]",%


No wonder, then, that TeX gets confused if "your friend" tries to also define that character as a shorthand for inline Python code. A character can't act both as a shorthand for inline code and as a delimiter, because listings has no way of knowing which function a given occurence of that character is supposed to fulfill.

You should tell "your friend" to choose a character that is less likely to occur in Python code, such as a backtick. For the specifics of the error, see David's answer.

-
All the "your friend"s made me smile :) but it really is a friend of mine this time. That actually makes a lot of sense, and gives a lot of traction to the    suggestion (since    isn't used in many languages at all). – Sean Allred Apr 10 '14 at 17:19
@SeanAllred I was just teasing you :) – Jubobs Apr 10 '14 at 17:59
@SeanAllred For information, if you want to insert a backtick in a TeX.SE comment, you need to escape it with a backslash: . – Jubobs Apr 10 '14 at 19:24
@SeanAllred In that case, backtick backslash backtick backtick gives you what you want: . – Jubobs Apr 15 '14 at 16:30
Now why didn't I think of that!? ` Thanks! – Sean Allred Apr 15 '14 at 16:41