# Problem with hyphens in listings

The problem is that in the inline listing "ABC-ABC" the hyphen disappears. It works if I remove the literate=... parameter but I need it because I want to wrap around long inline code by specifying manual hyphenation markers with "-". How can I solve this problem?

Another weird thing is that inline listing only works with \newcommand as the second part in the example shows. Is this a bug?

\documentclass{article}

\usepackage{listings}

\lstdefinestyle{Inline}
{
literate={\-}{}{0\discretionary{-}{}{}}
}
\newcommand{\code}[1]{\lstinline[style=Inline]{#1}}

\begin{document}
\code{ABC-ABC}\\
\code{AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABC\-ABC}\\[1cm]
Bug in listings???\\
\code{AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABC\-ABC}\\
\lstinline[style=Inline]{AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABC\-ABC}
\end{document}

-
You should avoid capturing the argument in \code. Rather try: \newcommand{\code}{\lstinline[style=Inline]} –  Werner Mar 30 '14 at 14:14
Then I get ...AAAAABC\ABC instead of ...AAAAABCABC or ...AAAAABC-<new line>ABC. –  user3389757 Mar 30 '14 at 14:17

There are two issues:

• \- is the same as - in literate, if you want to have a backslash in front of the hyphen, then the backslash needs to be escaped by a backslash. The hyphen does not need to be escaped by a backslash: \\- catches the backslash and the hyphen.

From the documentation:

4.1 How to read the reference
...
5. If you want to enter one of the special characters {}#%\, this character must
be escaped with a backslash. This means that you must write \} for the
single character ‘right brace’—but of course not for   the closing paramater
character.

• Package listings reads the code with changed catcodes. If the code for \lstinline is already read as argument, the argument is tokenized and catcode changes do not have an effect, see Werner's comment:

\newcommand*{\code}{\lstinline[style=Inline]}


Example:

\documentclass{article}
\usepackage[a5paper]{geometry}

\usepackage{listings}

\lstdefinestyle{Inline}
{
literate={\\-}{}{0\discretionary{-}{}{}}
}
\newcommand{\code}{\lstinline[style=Inline]}

\begin{document}
\noindent
\code{ABC-ABC}\\
\code{AAAAAAAAAAAAAAAAAAAAAAAAAABC\-ABC}\\
\code{AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABC\-ABC}
\end{document}


-