1

When using lstlisting with escapeinside, it seems to produce unintended spaces

\documentclass{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}

\usepackage{listings}
\lstset{basicstyle=\ttfamily,
    language=C,
    breaklines,
    frame=leftline,
    numberfirstline=true,
    numbers=left,
    tabsize=4,
    %           xleftmargin = 0pt,
    %           xrightmargin = 30pt,
    %           breakindent = 20pt,
    numberblanklines=true,
    escapeinside={?}{?},
}   
\let\origthelstnumber\thelstnumber      
\renewcommand{\lstlistingname}{Quellcode}
\newcommand*\rctnr[1]{%
    \setcounter{lstnumber}{\numexpr#1-1\relax}
    %       \lst@AddToHook{OnNewLine}{%
    %           \let\thelstnumber\origthelstnumber%
    %           \refstepcounter{lstnumber}%
    %       }%
}

\begin{document}
     % example 1
Example 1
\begin{lstlisting}[firstnumber=116, captionpos=b]
if (cmmnd == 31) {    ?\rctnr{118}?
    DIGITAL_IO_SetOutputLow(&DIGITAL_BOOSTC_EN);        ?\label{lhst:A}?
    PWM_CCU8_SetDutyCycleSymmetric(&PWM_BOOSTC_L,XMC_CCU8_SLICE_COMPARE_CHANNEL_1, value * 100);?\label{lhst:B} \rctnr{124}?
    DIGITAL_IO_SetOutputHigh(&DIGITAL_BOOST_DISP);  ?\label{lhst:C}
}
\end{lstlisting}
     % example 2
Example 2
\begin{lstlisting}[firstnumber=116, captionpos=b]
if (cmmnd == 31) {    ?\rctnr{118}?
    DIGITAL_IO_SetOutputLow(&DIGITAL_BOOSTC_EN);        ?\label{lhst:A}?
    PWM_CCU8_SetDutyCycleSymmetric(&PWM_BOOSTC_L,XMC_CCU8_SLICE_COMPARE_CHANNEL_1, value * 100);
    ?\label{lhst:B} \rctnr{124}?DIGITAL_IO_SetOutputHigh(&DIGITAL_BOOST_DISP);  ?\label{lhst:C}
}
\end{lstlisting}
\end{document}

I'd like to change the linenumber... that's worked quite well so far... but escaping into LaTeX obviously produces extra spaces, otherwise I would not get another line, nor would the indentation of the last line be off, as you can see in the second exammple.

Would be great, if anyone could provide some clues on what I am missing here or how to implement a workaround.

4
  • 1
    Just write \label{lhst:B}\rctnr{124} instead of \label{lhst:B} \rctnr{124} (that is, remove the space before \rctnr).
    – CarLaTeX
    Oct 17, 2017 at 10:45
  • 1
    also \rctnr always produces a space as you are missing a % after the setcounter Oct 17, 2017 at 10:45
  • @DavidCarlisle Don't tell this to egreg!
    – CarLaTeX
    Oct 17, 2017 at 10:47
  • Thanks a lot! I am sorry for wasting time with this easy errors :/
    – Pratched
    Oct 20, 2017 at 14:09

1 Answer 1

2

You have three errors in your code:

  1. In the definition of \rctnr you missed a %
  2. At the end of example 1 you wrote ?\label{lhst:C}. There is a closing ? missing ...
  3. At the end of example 2 you wrote ?\label{lhst:C}. There is a closing ? missing ...

Please see the following MWE:

\documentclass{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}

\usepackage{listings}
\lstset{basicstyle=\ttfamily,
    language=C,
    breaklines,
    frame=leftline,
    numberfirstline=true,
    numbers=left,
    tabsize=4,
    %           xleftmargin = 0pt,
    %           xrightmargin = 30pt,
    %           breakindent = 20pt,
    numberblanklines=true,
    escapeinside={?}{?},
}   
\let\origthelstnumber\thelstnumber      
\renewcommand{\lstlistingname}{Quellcode}
\newcommand*\rctnr[1]{%
    \setcounter{lstnumber}{\numexpr#1-1\relax}% <=======================
    %       \lst@AddToHook{OnNewLine}{%
    %           \let\thelstnumber\origthelstnumber%
    %           \refstepcounter{lstnumber}%
    %       }%
}

\begin{document}
     % example 1
Example 1
\begin{lstlisting}[firstnumber=116, captionpos=b]
if (cmmnd == 31) {    ?\rctnr{118}?
    DIGITAL_IO_SetOutputLow(&DIGITAL_BOOSTC_EN);        ?\label{lhst:A}?
    PWM_CCU8_SetDutyCycleSymmetric(&PWM_BOOSTC_L,XMC_CCU8_SLICE_COMPARE_CHANNEL_1, value * 100);?\label{lhst:B} \rctnr{124}?
    DIGITAL_IO_SetOutputHigh(&DIGITAL_BOOST_DISP);  ?\label{lhst:C}? 
}
\end{lstlisting}
     % example 2
Example 2
\begin{lstlisting}[firstnumber=116, captionpos=b]
if (cmmnd == 31) {    ?\rctnr{118}?
    DIGITAL_IO_SetOutputLow(&DIGITAL_BOOSTC_EN);        ?\label{lhst:A}?
    PWM_CCU8_SetDutyCycleSymmetric(&PWM_BOOSTC_L,XMC_CCU8_SLICE_COMPARE_CHANNEL_1, value * 100);
    ?\label{lhst:B} \rctnr{124}?DIGITAL_IO_SetOutputHigh(&DIGITAL_BOOST_DISP);  ?\label{lhst:C}?
}
\end{lstlisting}
\end{document}

you get the result:

resulting pdf

1
  • thanks a lot to you as well. I must have mixed something up when creating this MWE.
    – Pratched
    Oct 20, 2017 at 14:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .