2

I followed few answers mentioned here to highlight the XML content in my latex and tried to combine best things together from different answers but for some reason nothing is being highlighted in my XML? Can someone please help me how to make XML content in my latex to look appealing? I tried the following answer: https://tex.stackexchange.com/a/11134/226278

My XML is quite large and spans over 2-3 pages but now it's being displayed in a frame with line number so looks nice but none of the tags or strings is being highlighted except for some strings in the XML header. I would like to highlight the tags and strings in them so it would look nicer.

\usepackage{listings}
\usepackage{color}
\definecolor{gray}{rgb}{0.4,0.4,0.4}
\definecolor{darkblue}{rgb}{0.0,0.0,0.6}
\definecolor{cyan}{rgb}{0.0,0.6,0.6}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\geometry{a4paper}

 \lstset{
        language=xml,
        tabsize=3,
        %frame=lines,
        caption=Bulk test data events for rail sector
        label=code:rail1,
        frame=shadowbox,
        xleftmargin=20pt,
        framexleftmargin=15pt,
        numbers=left,
        numberstyle=\tiny,
        numbersep=5pt,
        breaklines=true,
        showstringspaces=false,
        basicstyle=\footnotesize,
        basicstyle=\ttfamily,
        columns=fullflexible,
        showstringspaces=false,
        basicstyle=\ttfamily\footnotesize,
        morestring=[b]",
        moredelim=[s][\bfseries\color{Maroon}]{<}{\ },
        moredelim=[s][\bfseries\color{Maroon}]{</}{>},
        moredelim=[l][\bfseries\color{Maroon}]{/>},
        moredelim=[l][\bfseries\color{Maroon}]{>},
        morecomment=[s]{<?}{?>},
        morecomment=[s]{<!--}{-->},
        commentstyle=\color{DarkOliveGreen},
        stringstyle=\color{blue},
        identifierstyle=\color{red},
    }
    \lstinputlisting{pics/RailSector.xml}

For example this is my sample XML file which I want to highlight and present in the pics folder:

<?xml version="1.0"?>
<hello:mydocument>
  <manufacturing>court</manufacturing>
  <bend>grabbed</bend>
  <quick>613049078</quick>
  <am>
    <brief>-2019050644</brief>
    <judge>955087623.417459</judge>
    <birds>green</birds>
    <join>among</join>
    <slight>
      <box>loose</box>
      <butter>table</butter>
      <afternoon>good</afternoon>
      <suddenly>rear</suddenly>
      <kitchen>copper</kitchen>
      <locate>802538930</locate>
    </slight>
    <wagon>1157100273.040341</wagon>
  </am>
  <this>1903268890</this>
  <rule>-1380139207</rule>
</hello:mydocument>
  • 1
    Welcome to TeX.SE. We can't do much without pics/RailSector.xml. Could you adjust your example to use the lstlisting environment? I assume that it would have the same highlighting issue that you're trying to solve. – Teepeemm Oct 11 at 19:22
  • @Teepeemm Thanks for the response. I have added a sample XML file content in the above code. What is the difference between lstset and lstlisting? is it possible to do the highlighting in the above code itself? – BATMAN_2008 Oct 11 at 19:59
  • 1
    Just a silly question: is that your whole document? Do you have a \begin{document} and an \end{document} ? – Elad Den Oct 11 at 20:44
  • Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – DG' Oct 11 at 20:52
3

After correcting two things in your MWE (adding \documentclass{article}, \begin{document}, and \end{document}) It seemed like nothing was being highlighted besides the first line of your text.

This is because everything was defined in one color (Maroon) while the comments (defined by <? ?>) were different.

changing the Maroon color did not make everything highlighted. For that you need to tell listings to look in the text by adding * in the lstset.

MWE:

\documentclass{article}
\usepackage{listings}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}

 \lstset{
        language=xml,
        tabsize=3,
        %frame=lines,
        caption=Bulk test data events for rail sector
        label=code:rail1,
        frame=shadowbox,
        xleftmargin=20pt,
        framexleftmargin=15pt,
        numbers=left,
        numberstyle=\tiny,
        numbersep=5pt,
        breaklines=true,
        showstringspaces=false,
        basicstyle=\footnotesize,
        basicstyle=\ttfamily,
        columns=fullflexible,
        showstringspaces=false,
        basicstyle=\ttfamily\footnotesize,
        morestring=[b]",
        moredelim=*[s][\bfseries\color{Maroon}]{<}{\ },
        moredelim=*[s][\bfseries\color{blue}]{</}{>},
        moredelim=*[l][\bfseries\color{red}]{/>},
        moredelim=*[l][\bfseries\color{Maroon}]{>},
        morecomment=[s]{<?}{?>},
        morecomment=[s]{<!--}{-->},
        commentstyle=\color{DarkOliveGreen},
        stringstyle=\color{blue},
        identifierstyle=\color{red},
    }
    
\begin{document}
    \lstinputlisting{RailSector.xml}
\end{document}

As for the question in the comments. \lstset is the settings definition. You tell the package what language you're using and what the keywords highlights are.

You then can type the code using lstlisting environment or input it from file (as you did) using \lstinputlisting

Using lstlisting is considered better practice WHEN ASKING A QUESTION HERE because it allows us to see the entire code without jumping between files. Here's an example of a MWE with the code in lstlisting

\documentclass{article}
\usepackage{listings}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}

 \lstset{
        language=xml,
        tabsize=3,
        morestring=[b]",
        moredelim=*[s][\bfseries\color{Maroon}]{<}{\ },
        moredelim=*[s][\bfseries\color{blue}]{</}{>},
        moredelim=*[l][\bfseries\color{red}]{/>},
        moredelim=*[l][\bfseries\color{Maroon}]{>},
        morecomment=[s]{<?}{?>},
        morecomment=[s]{<!--}{-->},
        commentstyle=\color{DarkOliveGreen},
        stringstyle=\color{blue},
        identifierstyle=\color{red},
    }
    
\begin{document}
    \begin{lstlisting}
<?xml version="1.0"?>
<hello:mydocument>
  <manufacturing>court</manufacturing>
  <bend>grabbed</bend>
  <quick>613049078</quick>
  <this>1903268890</this>
  <rule>-1380139207</rule>
</hello:mydocument>
    \end{lstlisting}
\end{document}
    
| improve this answer | |
  • This worked. Thanks a lot :) – BATMAN_2008 Oct 12 at 6:52

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.