10

I want to put some LaTeX source code in my PDF output with the style like this:

enter image description here

Color in text mode is white, in math mode is yellow and in comments is grey. For control sequences, I divide it into four kinds with different highlight mode:

  • Commands in text mode are cyan, and its argument(s) (non-optional) is/are orange and italic.
  • Commands in text mode are cyan, but its argument(s) (non-optional) is/are white and upright.
  • Commands in text mode are rose-color, and its optional argument(s) is/are orange and italic, non-optional argument(s) is/are cyan and italic.
  • Commands in math mode are purple, its arguments (optional or non-optional) should be rose-color and upright.

Remember that backslash before a command is always highlighted with the same color of the command, but delimiters of arguments remains white (color of body text).

I tried and searched the web for about ten hours, but still failed. Any hints or clues would be appreciated.

jub0bs
  • 58,916
  • 4
    minted is probably you best bet. – You Dec 01 '13 at 12:08
  • Might be of your interest: http://tex.stackexchange.com/questions/102596/minted-vs-texments-vs-verbments – juliohm Dec 01 '13 at 14:12
  • http://tex.stackexchange.com/q/8230/19356 might help. – kiss my armpit Dec 01 '13 at 20:45
  • @You Thank you for you comment, and sorry for my late, since my computer shut down for several days. I've read the documentation of minted, however, I couldn't compile a simple example provided by it. What's worse, I'm not familiar with Python, so.. Should I open another question for those problems? – Tachibana Kanade Dec 06 '13 at 03:22
  • @juliohm It seems that most people prefer Python-based packages, such as minted, or other script-based packages (am I right?). The reason, I guess, is that these script languages provide much more controlling and conditional switching than LaTeX itself does. I will follow your advice to study these packages when I get rid of my current article. :) – Tachibana Kanade Dec 06 '13 at 03:34
  • @DonutE.Knot Thank you for your kindly help! However, LaTeX code and the code displayed in the link you provided differ greatly. Since LaTeX put arguments in braces ({}), optional ones in square brackets ([]) and start a control sequence with a backslash (\), situation goes much knottier. – Tachibana Kanade Dec 06 '13 at 03:43
  • @DonutE.Knot BTW, do you know how to mark a backslash as code in Markdown? – Tachibana Kanade Dec 06 '13 at 03:45
  • @TachibanaKanade Your specs are not self-consistent; in particular, your first two bullet points conflict. listings can do a lot, but nothing can be done without precise highlighting specifications. – jub0bs Mar 18 '14 at 16:08
  • @TachibanaKanade Install Python and Pygment and you will be able to use minted (note that Python package installations don't require any Python knowledge at all!). When that is completed, using monokai theme will give you the desired output. – fractal Mar 16 '20 at 13:41

1 Answers1

4

This solution isn't perfect (hence the community wiki), but it might get you or someone else started (if anyone would like to improve this solution, please feel free).

screenshot

I've borrowed some code from Emphasize (color) contents between two delimiters in listings, but not the delimiters themselves to help the grouping delimiters.

It's a tricky problem because you essentially wish to change the listings style midway through the listings (I tried a few things, but couldn't get it working).

% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass{article}
\usepackage{listings}           
\usepackage{xcolor}

% https://tex.stackexchange.com/questions/147836/emphasize-contents-between-two-with-listings
\def\begindollar{\ttfamily\$\bgroup\color{yellow}\aftergroup\enddollar}
\def\enddollar{\$\egroup}

% tweaked from above for { }
\def\beginbrace{\ttfamily\color{white}{\{}\bgroup\color{orange}\itshape\aftergroup\endbrace}
\def\endbrace{\color{white}{\}}\egroup}

% tweaked from above for [ ]
\def\beginbracket{\ttfamily\color{white}{[}\bgroup\color{orange}\itshape\aftergroup\endbracket}
\def\endbracket{\color{white}{]}\egroup}
\lstset{%
    basicstyle=\ttfamily\color{orange},language={[LaTeX]TeX},   
    texcsstyle=*\color{cyan},
    columns=flexible,
    backgroundcolor=\color{black},
    commentstyle=\color{gray},    % comments
    literate={usepackage}{\bgroup\color{purple}{usepackage}\egroup}1
    {documentclass}{\bgroup\color{purple}{documentclass}\egroup}1,
    moredelim = **[is][\begindollar]{\$}{\$},
    moredelim=**[is][\beginbrace]{\{}{\}},
    moredelim=**[is][\beginbracket]{[}{]},
}


\begin{document}

\begin{lstlisting}
\documentclass[10pt]{article}
\usepackage{tikz}
\begin{document}
\tikz \color{blue} $(a+b)$ $n \choose r$ % \tikz
\end{document}
\end{lstlisting}

\end{document}
cmhughes
  • 100,947
  • Oh, this is a fantastic work! However, it has some foibles, such as the backslash signs were colored differ from the following control sequence, and command(s) in math environment does/do not color as a special part. I will read the documentation more carefully and try to improve this answer later. Again, thank you very much! :) – Tachibana Kanade Dec 06 '13 at 03:50
  • @TachibanaKanade yes indeed, it isn't perfect :) if you can improve it, please feel free! – cmhughes Dec 06 '13 at 04:48