7

I'm writing a user manual with the documentation of some code written in XML. I would like to do this documentation within two nested description environment, highlighting the labels with my XML syntax style, as in the following code:

\documentclass[11pt,%                   
           a4paper,%                   
           twoside,openright,%        
           ]{scrreprt}         
\usepackage{listings,xcolor}
\definecolor{darkgreen}{rgb}{0.0,0.6,0.0}
\usepackage{lipsum}
\lstdefinelanguage{XML}
{
morestring=[b]",
morestring=[s]{>}{<},
morecomment=[s]{<?}{?>},
morecomment=[s]{<!--}{-->},
moredelim=[l][basicstyle]{>},
morekeywords={xmlns,version,type,value,Value,app,com}
}
\lstdefinestyle{MyXML}
{
    language=XML,
    basicstyle=\small\ttfamily,
    keywordstyle=\color{darkgreen},
    identifierstyle=\color{blue},
    stringstyle=\color{red},    
    commentstyle=\color{gray}\upshape,
    emphstyle=\itshape,
    columns=fullflexible,
    breaklines=true,    
    keepspaces=true,
    showstringspaces=false, 
    frame=single, 
}

% Use lstinline as item in description
\newcommand*{\lstitem}[2][]{
    \setbox0\hbox{\lstinline[#1]|#2|}  
    \item[\usebox0]\leavevmode
}

\renewcommand{\ttdefault}{pcr}

\begin{document}

\begin{description}
    \lstitem[style=MyXML]{time} 
    \begin{description}
        \lstitem[style=MyXML,morekeywords={app}]{app}\lipsum{1}
        \lstitem[style=MyXML,morekeywords={com}]{com}\lipsum{2}
    \end{description}
    \lstitem[style=MyXML]{standalone}\lipsum{3}
\end{description}

\end{document}

I would like that the first label of the nested description ("app") remains on the same line of the "time" label. Can someone help me?

5
+50

You should not absorb the text for \lstinline as an argument too early, because this disrupts the working of the macro that needs to change category codes.

A way out is to patch an internal macro called by \lstinline so that it closes the group initiated by \setbox0=\hbox\bgroup and then delivers \item[\usebox0].

In the following implementation, the patch to \lst@InlineM is local to the implicit group the box is built in, so it won't influence other usages of \lstinline.

\documentclass[
  11pt,
  a4paper,
  twoside,
  openright,
]{scrreprt}
\usepackage{listings,xcolor,etoolbox}
\definecolor{darkgreen}{rgb}{0.0,0.6,0.0}
\usepackage{lipsum}

\lstdefinelanguage{XML}{
  morestring=[b]",
  morestring=[s]{>}{<},
  morecomment=[s]{<?}{?>},
  morecomment=[s]{<!--}{-->},
  moredelim=[l][basicstyle]{>},
  morekeywords={xmlns,version,type,value,Value,app,com}
}
\lstdefinestyle{MyXML}{
  language=XML,
  basicstyle=\small\ttfamily,
  keywordstyle=\color{darkgreen},
  identifierstyle=\color{blue},
  stringstyle=\color{red},
  commentstyle=\color{gray}\upshape,
  emphstyle=\itshape,
  columns=fullflexible,
  breaklines=true,
  keepspaces=true,
  showstringspaces=false,
  frame=single,
}

% Use lstinline as item in description
\makeatletter
\newcommand*{\lstitem}[1][]{%
  \setbox0\hbox\bgroup
    \patchcmd{\lst@InlineM}{\@empty}{\@empty\egroup\item[\usebox0]\leavevmode\ignorespaces}{}{}%
    \lstinline[#1]%
}
\makeatother

\renewcommand{\ttdefault}{pcr}

\begin{document}

\begin{description}

\lstitem[style=MyXML]|time|
  \begin{description}
  \lstitem[style=MyXML,morekeywords={app}]{app}\lipsum[1]
  \lstitem[style=MyXML,morekeywords={com}]{com}\lipsum[2]
  \end{description}
\lstitem[style=MyXML]{standalone}\lipsum[3]
\end{description}

\end{document}

enter image description here

|improve this answer|||||
  • First of all, thanks for the useful suggestion. But, what I need is to move the nested description environment one line up, so that the "app" label is placed next to the label "time": do you have any ideas to do that? – Davide Jun 10 '15 at 12:47
  • 1
    @Davide I'm afraid that deep surgery is necessary in listings for this to work. I've come to the conclusion that it's listings' fault if removing \leavevmode misplaces the description items. – egreg Jun 10 '15 at 13:51
  • @Davide '\lstinline makes global assignments to some variables; these are not wiped out until the item has been typeset, but unfortunately \item does not act immediately and defers it to the start of the next paragraph. This has the side effect that the outer label is misplaced to overprint the inner one – egreg Jun 10 '15 at 14:08

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.