14

I'm trying to make a pdf language definition for the listings package, but I can't get the string definition to work correctly. This is what I have defined:

\lstdefinelanguage{pdf}{
alsoletter={/,(,)},
morekeywords={/Type,/Outlines, /Count, /Kids, /Page,
    /Parent, /Resources, /MediaBox, /Contents, /Length, /ProcSet, /PDF, /Size,
    /Root, /Pages, /Catalog, /Font, /F1, /F2, /Text, /Subtype, /Type1, /Name,
    /BaseFont, /Helvetica-Bold, /Encoding, /MacRomanEncoding, /Helvetica},
morecomment=[l]{\%},
morestring=[s]{(}{)}
}

Here you can see the result:

enter image description here

It should either go from ( to ), or maybe from BT to ET (which is what vim does).


Okay. So I figured it out. For some reasons using breaklines=true creates this error.

Here is a minimal working example that produces the error:

\documentclass[a4paper,english]{article}
\usepackage[T1]{fontenc}               
\usepackage[utf8]{inputenc}             
\usepackage{babel, listings, color}
\definecolor{darkviolet}{rgb}{0.5,0,0.4}
\definecolor{darkgreen}{rgb}{0,0.4,0.2} 
\definecolor{darkblue}{rgb}{0.1,0.1,0.9}

\lstset{ %
    keywordstyle=\color{darkviolet},
    commentstyle=\color{darkgreen},
    stringstyle=\color{darkblue},
    breaklines=true,% Produces the error in the highlighting
}

\lstdefinelanguage{pdf}{
    alsoletter={/},
    morekeywords={/Length, /F1},
    morecomment=[l]{\%},
    morestring=[s]{(}{)}
}

\begin{document}
\begin{lstlisting}[language=pdf]
%PDF-1.0

5 0 obj
<< /Length 44 >>        
stream
BT
/F1 27 Tf
100 100 Td (Hello World) Tj
ET
endstream
endobj

%%EOF
\end{lstlisting}
\end{document}

But I need to have breaklines set to true. So anybody know a reason why this is happening? Or is it just a bug in the listings package?

4

There appears top be special code "currently only for )" when breakline is active (listings.dtx 1.4, l. 12576):

% \begin{macro}{\lst@breakProcessOther}
% is the same as |\lst@ProcessOther| except that it also outputs the current
% token string. This inserts a potential linebreak point.
% Only the closing parenthesis uses this macro yet.
%    \begin{macrocode}
\gdef\lst@breakProcessOther#1{\lst@ProcessOther#1\lst@OutputOther}
\lst@AddToHook{SelectCharTable}
    {\lst@ifbreaklines \lst@Def{`)}{\lst@breakProcessOther)}\fi}
%    \end{macrocode}

There are probably better ways to remove that from the hook again, but this seems to do, when put in the preamble:

\makeatletter
\def\uglypatch #1\lst@ifbreaklines#2\fi#3\uglypatch{%
  \long\def\lsthk@SelectCharTable{#1#3}%
}
\lst@ifbreaklines
\expandafter\uglypatch\lsthk@SelectCharTable\uglypatch
\fi
\makeatother
| improve this answer | |

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.