Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

1 Answer 1

up vote 8 down vote accepted

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}

Result

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.