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.

I'm trying to use the line range markers from the listings package to include sections of code. I'd previously been using actual line numbers, but based on the answer to this question, I've been able to gradually transition to using markers. It seems that if multiple line comment sequences have been defined (e.g., with comment=[l]{;},morecomment=[l]{;;}) as is sometimes necessary (see this answer), and the texcl option is used, then line range prefixes that are comments must include the longest such comment form.

For instance, the following MWE defines ; and ;; as introducing comments, and uses the texcl option, but does not compile.

\documentclass{article}
\usepackage{listings}

\begin{document}

\lstset{%
  texcl=true,%
  comment=[l]{;},%
  morecomment=[l]{;;},%
  rangeprefix=;MARK\ ,%
  includerangemarker=false,%
}

\begin{lstlisting}[linerange=lineTwo-lineFour]
lineOnee
;MARK lineTwo
lineThree
;MARK lineFour
lineFive
;MARK lineSix
lineSeven
\end{lstlisting}

\begin{lstlisting}[linerange=5-7]
lineOne
;MARK lineTwo
lineThree
;MARK lineFour
lineFive
;MARK lineSix
lineSeven
\end{lstlisting}

\end{document}

The error is

ERROR: Undefined control sequence.

--- TeX said ---
\lst@next ->\lst@c;1 M

l.26 ;M
       ARK lineFour
--- 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.

Commenting out the texcl=true option will make this example compile, but I need the texcl option. Alternatively, changing the rangeprefix to ;;MARK\ (and updating the listed code appropriately) also works, and is the workaround I'm using. I am fine with this workaround, but I'm wondering whether there is a way to use texcl, ; and ;; as comments, and have a range prefix begin with a single ;.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

The issue seems to be related to be partially related to the lack of space between your ; and MARK. One work around is to make sure you mark lines are formatted in the same way as the simple comments, i.e., as ; MARK. Note that these range markers are an experimental feature of listings.

Sample output

\documentclass{article}
\usepackage{listings}

\begin{document}

\lstset{%
  texcl=true,%
  comment=[l];,%
  morecomment=[l];;,%
  rangeprefix=;\ MARK\ ,%
  includerangemarker=false}

\begin{lstlisting}[linerange=lineTwo-lineFour]
lineOnee
; MARK lineTwo
lineThree
; MARK lineFour
lineFive
; MARK lineSix
lineSeven
\end{lstlisting}

\begin{lstlisting}[linerange=5-7]
lineOne
; simple comment on line two
; MARK lineThree
lineFour
lineFive
; MARK lineSix
lineSeven
\end{lstlisting}

\lstset{rangeprefix=;;MARK\ ,}
\begin{lstlisting}[linerange=5-7]
lineOne
; simple comment on line two
;;MARK lineThree
lineFour
lineFive
;;MARK lineSix
lineSeven
\end{lstlisting}

\end{document}
share|improve this answer
    
Good catch on the spaces, I hadn't thought to try modifying the spacing. Any idea why issue this didn't seem to come up in the earlier questions, i.e., why using ;;MARK ... didn't cause a problem? –  Joshua Taylor Jun 12 '13 at 18:51
    
It is only an issue when (a) you have a range markers set that do fit partially but not exactly with the comment style and (b) try to use a numerical range of lines that includes a line with such a marker. –  Andrew Swann Jun 12 '13 at 18:58
    
By the way, this was the reason that I had such an hard time applying the previous solution to my document. I had a number of \lstlistings to convert from line numbers to range markers. I added the range markers and updated the first \lstlisting, but that pushed up the line numbers, so the code in the second \lstlisting that still used line numbers then contained a range marker. –  Joshua Taylor Jun 12 '13 at 19:07

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.