0

Using custom listings to markup my XML in beamer slides, I want to separate the attribute from the element, making attribute green rather than blue. I also want to make the black signs light gray. How do I accomplish this? I'm not using minted, but listings.

enter image description here

Edit: Here is my code for reproduction:

\documentclass[10pt, compress]{beamer}

\usepackage{booktabs}
\usepackage[scale=2]{ccicons}
\usepackage[english]{babel}
\usepackage{listings}
\usepackage{color}
\usepackage{xcolor} 

\definecolor{dkgreen}{RGB}{151,205,160}
\definecolor{test}{RGB}{255,212,212}
\definecolor{mauve}{RGB}{255,187,185}
\definecolor{gray}{rgb}{0.4,0.4,0.4}
\definecolor{darkblue}{rgb}{0.0,0.0,0.6}
\definecolor{lightblue}{RGB}{176,223,224}
\definecolor{cyan}{rgb}{0.0,0.6,0.6}
\definecolor{darkred}{rgb}{0.6,0.0,0.0}
\definecolor{light-gray}{RGB}{77,77,77}
\definecolor{numbers}{RGB}{255,153,29}
\definecolor{whitewhite}{RGB}{214,214,214}


\lstset{
  basicstyle=\ttfamily\footnotesize,
  columns=fullflexible,
  showstringspaces=false,
  numbers=left,                   % where to put the line-numbers
  numberstyle=\tiny\color{numbers},  % the style that is used for the line-numbers
  stepnumber=1,
  numbersep=5pt,                  % how far the line-numbers are from the code
  backgroundcolor=\color{light-gray},      % choose the background color. You must add \usepackage{color}
  showspaces=false,               % show spaces adding particular underscores
  showstringspaces=false,         % underline spaces within strings
  showtabs=false,                 % show tabs within strings adding particular underscores
  frame=none,                   % adds a frame around the code
  rulecolor=\color{black},        % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. commens (green here))
  tabsize=2,                      % sets default tabsize to 2 spaces
  captionpos=b,                   % sets the caption-position to bottom
  breaklines=true,                % sets automatic line breaking
  breakatwhitespace=false,        % sets if automatic breaks should only happen at whitespace
  title=\lstname,                   % show the filename of files included with \lstinputlisting;
                              % also try caption instead of title  
  commentstyle=\color{gray}\upshape
}


\lstdefinelanguage{XML}
{
  morestring=[s][\color{mauve}]{"}{"},
  morestring=[s][\color{whitewhite}]{>}{<},
  morecomment=[s]{<?}{?>},
  morecomment=[s][\color{dkgreen}]{<!--}{-->},
  stringstyle=\color{orange},
  identifierstyle=\color{lightblue},
  keywordstyle=\color{test},
  morekeywords=       {xmlns,xsi,noNamespaceSchemaLocation,type,id,x,y,source,target,version,tool,transRef,roleRef,objective,eventually,skos}% list your attributes here
}
\begin{document}

   \begin{frame}[fragile]
  \frametitle{}

\begin{lstlisting}[language=xml]
<element attribute="#attributevalue" />
\end{lstlisting}

\end{frame}

\end{document}
  • Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – samcarter_is_at_topanswers.xyz May 10 '18 at 16:11
  • 1
    you can use \lstset{} as described in listings documentation, section 1.3 and further – naphaneal May 10 '18 at 16:20
1
  • Colouring attribute in a different colour can be achieved by creating, for example, a new keywordstyle for it (keyword is just an example, as I don't know enough about XML to judge which category would be fitting).

  • to change the colour of the normal text (= etc.) you can change the basic style

  • Off-topic: you don't need color when you load xcolor and in fact you don't need to load neither of both with beamer


\documentclass[10pt, compress]{beamer}

\usepackage{booktabs}
\usepackage[scale=2]{ccicons}
\usepackage[english]{babel}
\usepackage{listings}
%\usepackage{color}
%\usepackage{xcolor} 

\definecolor{dkgreen}{RGB}{151,205,160}
\definecolor{test}{RGB}{255,212,212}
\definecolor{mauve}{RGB}{255,187,185}
\definecolor{gray}{rgb}{0.4,0.4,0.4}
\definecolor{darkblue}{rgb}{0.0,0.0,0.6}
\definecolor{lightblue}{RGB}{176,223,224}
\definecolor{cyan}{rgb}{0.0,0.6,0.6}
\definecolor{darkred}{rgb}{0.6,0.0,0.0}
\definecolor{light-gray}{RGB}{77,77,77}
\definecolor{numbers}{RGB}{255,153,29}
\definecolor{whitewhite}{RGB}{214,214,214}


\lstset{
  basicstyle=\ttfamily\footnotesize\color{white},
  columns=fullflexible,
  showstringspaces=false,
  numbers=left,                   % where to put the line-numbers
  numberstyle=\tiny\color{numbers},  % the style that is used for the line-numbers
  stepnumber=1,
  numbersep=5pt,                  % how far the line-numbers are from the code
  backgroundcolor=\color{light-gray},      % choose the background color. You must add \usepackage{color}
  showspaces=false,               % show spaces adding particular underscores
  showstringspaces=false,         % underline spaces within strings
  showtabs=false,                 % show tabs within strings adding particular underscores
  frame=none,                   % adds a frame around the code
  rulecolor=\color{black},        % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. commens (green here))
  tabsize=2,                      % sets default tabsize to 2 spaces
  captionpos=b,                   % sets the caption-position to bottom
  breaklines=true,                % sets automatic line breaking
  breakatwhitespace=false,        % sets if automatic breaks should only happen at whitespace
  title=\lstname,                   % show the filename of files included with \lstinputlisting;
                              % also try caption instead of title  
  commentstyle=\color{gray}\upshape
}


\lstdefinelanguage{XML}
{
  morestring=[s][\color{mauve}]{"}{"},
  morestring=[s][\color{whitewhite}]{>}{<},
  morecomment=[s]{<?}{?>},
  morecomment=[s][\color{dkgreen}]{<!--}{-->},
  stringstyle=\color{orange},
  identifierstyle=\color{lightblue},
  keywordstyle=\color{test},
  morekeywords=       {xmlns,xsi,noNamespaceSchemaLocation,type,id,x,y,source,target,version,tool,transRef,roleRef,objective,eventually,skos},% list your attributes here
  morekeywords=[2]{attribute},
  keywordstyle=[2]\color{green}
}
\begin{document}

   \begin{frame}[fragile]
  \frametitle{}

\begin{lstlisting}[language=xml]
<element attribute="#attributevalue" />
\end{lstlisting}
\end{frame}

\end{document}

enter image description here

| improve this answer | |
  • I see. You add another type of keyword style with distinct keywords and apply a color value to them. Many thanks! – phronesismimesis May 10 '18 at 16:47
  • @phronesismimesis That is the basic approach. I just don't know if keyword is the correct category because I don't know much about XML. – samcarter_is_at_topanswers.xyz May 10 '18 at 16:54
  • Well, one issue that I found was that when I specified the attribute "RDF" as green, the element "RDF" in a root tag went green as well, while I would like that to be blue. Not sure if there's a way around this. Basically I'd like to specify the colorscheme as per github.com/junegunn/seoul256.vim. – phronesismimesis May 10 '18 at 18:05
  • @phronesismimesis I am sorry, I have no idea what a root tag might be. – samcarter_is_at_topanswers.xyz May 10 '18 at 19:01
  • @phronesismimesis You're welcome! – samcarter_is_at_topanswers.xyz May 10 '18 at 19:20

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.