8

Here is my code:

\documentclass{article}
\usepackage{listings}
\lstset{basicstyle=\ttfamily,columns=fullflexible}
\begin{document}

\lstset{escapeinside={((:}{:))}}
\begin{lstlisting}
foo
((:\textit{bar}:))
baz
\end{lstlisting}

\lstset{escapeinside={((}{))}}
\begin{lstlisting}
foo
((\textit{bar}))
baz
\end{lstlisting}

\end{document}

Here is the output:

enter image description here

6

The package certainly allows for more than one open and close characters in escapeinside, for the code changes depending if there is one, two, or more than two characters. In the case of more than one <character> as delimiter, escapeinside works by making the first token in the open and close group active characters which look ahead and check if they are followed by the defined characters.

In the first example (let's assume, for simplicity, that you used escapeinside={(:}{:)}), ( is made an active character which looks ahead and checks if it is followed by a : character and, if so, starts the escaping. Inside the escaped code, : is made active and it looks for a ) character.

In the second example, ( is made an active character which looks ahead for another (, and starts the escaped code. Inside the escaped code, ) is made active, and looks ahead for a ). However it never finds a ) character, but a ) active character, so it doesn't end the escaped code and just prints a ) instead. The next ) does the same and prints another )1.

This problem does not show in your first example because the character made active and the character looked for are different, so it doesn't show the problem.

Here's a proposal to patch that issue and make escapeinside work with whatever character. Beware, though! I didn't test this code extensively (read: I didn't test at all except for the given code) so I don't know if fixing this will break something else. I'd say it won't because it's a very specific bit of code which doesn't seem to be used elsewhere, but the warning is here so you can't blame me afterwards :-)

I modified two definitions, \lst@CDefIt and \lst@IfNextChars@@, which are used with two token delimiters and three-or-more token delimiters, respectively. I changed the token comparison from \ifx to \if, which will ignore the catcode difference and will only compare the character itself, so it should do what you're looking for. I marked the two lines I changed. Adding this code to your preamble should do the trick:

\makeatletter
\def\lst@CDefIt#1#2#3#4#5#6#7#8{%
    \ifx\@empty#2\@empty
        \def#1{#6\def\lst@next{#7#4#8}\lst@next}%
    \else \ifx\@empty#3\@empty
        \def#1##1{%
            #6%
            % \ifx##1#2\def\lst@next{#7#4#8}\else % <--- Before
            \if\noexpand##1\noexpand#2\def\lst@next{#7#4#8}\else % <--- After
                     \def\lst@next{#5##1}\fi
            \lst@next}%
    \else
        \def#1{%
            #6%
            \lst@IfNextCharsArg{#2#3}{#7#4#8}%
                                     {\expandafter#5\lst@eaten}}%
    \fi \fi}
\def\lst@IfNextChars@@#1#2\relax#3{%
    \def\lst@tofind{#2}\lst@lAddTo\lst@eaten{#3}%
    % \ifx#1#3% <--- Before
    \if\noexpand#1\noexpand#3% <--- After
        \ifx\lst@tofind\@empty
            \let\lst@next\@tempa
        \else
            \let\lst@next\lst@IfNextChars@
        \fi
        \expandafter\lst@next
    \else
        \expandafter\@tempb
    \fi}
\makeatother

1 If you read carefully, you'll notice that the explanation is faulty because it doesn't make sense for the open code to work and the close code to fail if they do the same thing. Well, it doesn't :) I imagine that the open code is very different from the close code, thus one works and the other doesn't. To be honest debugging listings on Sunday is painful, so I assumed that part of the code works and didn't try too hard finding out why.

  • 2
    If only debugging listings were not painful during the week ... ;) – siracusa Jun 3 at 5:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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