0

Problem:

I am using a custom command to color keywords within the custom command \code. However, this will color everything blue as illustrated in MWE. I would like everything between < and > to be coloured in pink. The solution should only target the command \code.

Minimal Working Example (MWE):

\documentclass{scrreprt}
\usepackage{xcolor}
\usepackage{lipsum}

% --------------------------------------------------------------
% Colors
% --------------------------------------------------------------
\definecolor{editorBlue}{cmyk}{1, 0.35, 0, 0}
\definecolor{editorPink}{cmyk}{0, 1, 0, 0}

% ----------------------------------------------------------------------
%  Command for inline code
% ----------------------------------------------------------------------
\newcommand{\code}[1]{
  {\color{editorBlue}\texttt{#1}}
}

\begin{document}

\noindent Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut pur us elit, vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna. Donec vehicula augue eu neque. Pellentesque habitant morbi tristique senectus \code{<div>} et netus et malesuada fames ac tur pis egestas. Mauris ut leo. Cras viverra metus rhoncus sem. Nulla et lectus vestibulum urna f ringilla ultrices. Phasellus eu tellus sit amet tor tor gravida placerat. Integer sapien est, iaculis in, pretium quis, \code{<body>} viverra ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Curabitur auctor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit \code{<html>} amet orci dignissim r utr um \\

\noindent Desired output: \code{<{\color{editorPink}div}>}

\end{document}

Desired output:

The desired output is included in the MWE.

  • If all of your \code fragments contain <...>, you could simply move the characters part in the defintion of \code. Otherwise, if we are talking html, you might consider using the listings package and its command \lstinline which allows somewhat more advanced colouring. – Timm Mar 28 '17 at 11:27
  • Do you mean this: \newcommand{\code}[2][editorPink]{\textless\textcolor{#1}{#2}\textgreater} and in the document \code{div}(pink) or \code[blue]{div}(blue). – Bobyandbob Mar 28 '17 at 12:04
  • @Bobyandbob I am trying out the \lstinline option and I arrive to the following solution: \newcommand{\code}[1]{{\lstinline[basicstyle=\small\ttfamily\upshape]{#1}}} - but for some reason it's breaking the highlighting for the rest of the document. – kexxcream Mar 28 '17 at 12:09
  • Mistaken identity? @Timm ?. Is my comment relevant?(@kexxcream). – Bobyandbob Mar 28 '17 at 12:18
  • @Bobyandbob No, as it would take too much time to change all code inside the document. The lstinline option seems to do the trick, but mess up the highlighting at the same time. – kexxcream Mar 28 '17 at 12:20
1

So I tried my suggestion with the \lstinline option, and for me it seems to work:

\documentclass{scrreprt}
\usepackage{xcolor}
\usepackage{listings}

% --------------------------------------------------------------
% Colors
% --------------------------------------------------------------
\definecolor{editorBlue}{cmyk}{1, 0.35, 0, 0}
\definecolor{editorPink}{cmyk}{0, 1, 0, 0}

% ----------------------------------------------------------------------
%  Command for inline code
% ----------------------------------------------------------------------
\newcommand{\code}[1]{%
    {%
        \lstset{%
            basicstyle=\small\ttfamily‌​\upshape,
            keywordstyle=\color{editorBlue},
            commentstyle=\color{editorPink},
            keepspaces 
        }%
        \lstinline[language=HTML]{#1}%
    }%
}
\begin{document}

\noindent Standard: \code{<html>} \\
\noindent More: \code{<html>  <!-- Comment -->} \\
\noindent In Text: Pellentesque habitant morbi tristique senectus \code{<div>} et netus et malesuada fames ac tur pis egestas. Integer sapien est, iaculis in, pretium quis, \code{<body>} viverra ac, nunc. Curabitur auctor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit \code{<html>} amet orci dignissim r utr um \\

\end{document}

Gives the following result:

enter image description here

I moved the settings outside of the brackets, but that didn't really matter for me, it just looks nicer. In case you do not use listings anywhere else in the document, you could also move them to your preamble.

I also added some examples how to set colors, everything else should be in the package documentation.

  • This worked, but I had to tweak it with basicstyle=\ttfamily\upshape\lst@ifdisplaystyle\tiny\fi, as I am using a different style for code blocks and default style inline. In addition, I redefined the command to \newcommand{\code}[1]{\lstinline{#1}} – kexxcream Mar 28 '17 at 19:09
  • Well, of course you'll can tweak it to match your style, that's why there are the settings. Your redefinition apparently also works, however note that it now has no idea what language you are writing. – Timm Mar 28 '17 at 19:11
  • If I write \newcommand{\inlinecodes}[1]{\lstinline[language=HTML]{#1}} the highlighting breaks for some reason. What could cause this? Eventually I would want to switch out this to CSS. – kexxcream Mar 28 '17 at 19:14
  • Did you copy the exact code? If yes, please show your problem with an image or an errormessage. – Timm Mar 28 '17 at 19:36
  • Yes, it's the exact code. I'm using Texpad 1.7.36 for macOS. It's this [language=HTML] that disables the highlighting for the rest of the document. – kexxcream Mar 28 '17 at 19:59

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.