1

This example shows how to produce PDF slides with syntax highlighting of source code. The example uses beamer and (presumably) pdflatex.

Syntax-highlighted code

Another example shows syntax coloring for HTML output using htlatex. It's an unpleasant kludge

  1. it precludes calling pdflatex on the same LaTeX file
  2. one must experiment to find what ectt-1000 maps to
  3. one defines colors for css categories, not for code syntax categories

but it works.

The following code revisits this problem and asks to do better.

\documentclass{article}
\usepackage{color}
\usepackage{listings}
\usepackage[T1]{fontenc}

\lstset{
    basicstyle=\ttfamily,
    language=C++,
    keywordstyle=\rmfamily\bfseries,
    commentstyle=\sffamily,
}

\begin{document}

\Css{div.lstlisting .ectt-1000 {font-family: monospace;color:blue}}
\Css{div.lstlisting .ecss-1000 {font-family: monospace;color:green}}
\Css{div.lstlisting .ecbx-1000 {font-family: monospace;color:red}}

\section*{Inserting source code}
\begin{lstlisting}
#include<stdio.h>
#include<iostream>
// A comment
int main(void)
{
    printf("Hello World\n");
    return 0;
}
\end{lstlisting}

\end{document}

Now commenting out the \Css lines and running pdflatex gives one output

pdflatex output is syntax colored

and processing with htlatex gives another.

syntax coloring produced by htlatex

Can you suggest a way to make this kludge more usable by

  1. correlating the two "blue" colors—even though ultimately one is defined in LaTeX's color package and the other in HTML,
  2. making the \Css definitions point to colors through syntax category,
  3. making it possible to process the file with pdflatex while writing/for debugging (htlatex is considerably slower)

?

  • Adding "\Css{div.lstlisting .ectt-1000 {font-family: monospace;color:blue}}" is a nasty kludge, but you're right. Despite a reasonably long search before posting, the question you reference does not show up, but it's exactly what I'm after—minus the kludge. I'll delete if no one offers a better solution. – Calaf Jan 29 '15 at 18:14
  • 1
    Don't delete! Posting a duplicate question isn't a bad thing unless it clearly shows a "lack of research effort", something you've obviouosly done. The very fact that you searched a long time with no success is a testament to why your question is necessary. Now, if someone searches "LaTeX HTML syntax highlighting", your question is sure to pop up, leading the reader to answer they need. Ah, the genius of StackExchange :) – Sean Allred Jan 29 '15 at 18:38
  • @SeanAllred Thanks for the explanation. Even better then deleting or not deleting, I've changed the question to look for an actual solution, one that does not require hacking into the html file to find the css classes and then defining those blindly from what syntax categories they map to. – Calaf Jan 29 '15 at 21:50
  • Isn't that a duplicate of tex.stackexchange.com/questions/99500/… ? You should maybe have a look at overleaf.com/74567mmxwkw – Clément Feb 3 '15 at 0:34
0

Correct solution would be to emit html tags around each word in listings, or better, around block of same syntax style, with class attribute set according to syntax style.

Unfortunately, listings source code is complex and I can't figure out how to do that, so I will provide just a some helper to ease the color definition.

Create file textstyle4ht.sty:

\RequirePackage{xcolor}

% extract current color as hexadecimal value
\newcommand\tsf@getColor[1][.]{
    % colorname `.` holds current color
    \extractcolorspec{.}{\tsf@color}
    \expandafter\convertcolorspec\tsf@color{HTML}\tsf@color
    %\tmpcolor
}

% write css color for given css selector
\newcommand\CssColor[1]{%
    % save current color
    \tsf@getColor%
    \Css{#1{color:\#\tsf@color;}}%
}

with this package, we are able to get current color and save it to the css file.

In your example, you have \Css commands in the document. This is a bad style, correct is to provide a config file with tex4ht configurations.

hello.cfg:

\RequirePackage{textstyle4ht}
\Preamble{xhtml}
\begin{document}
\makeatletter
\newcommand\LstCss[2]{%
    \bgroup%
        \csname lst@#2\endcsname%
        \CssColor{#1}%
    \egroup%
}
\makeatother
\LstCss{div.lstlisting .ecbx-1000}{keywordstyle}
\LstCss{div.lstlisting .ecss-1000}{commentstyle}
\LstCss{div.lstlisting .ectt-1000}{basicstyle}
\EndPreamble

in this file, our textstyle4ht package is required, and new \LstCss command is provided. Difference between your version and this one is that real color defined for the style is used. You still need to find corresponding class attribute for each identifier style.

With slightly modified document:

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\usepackage[T1]{fontenc}

\lstset{
    basicstyle=\color{blue}\ttfamily,
    language=C++,
    keywordstyle=\color{red}\rmfamily\bfseries,
    commentstyle=\color{green}\sffamily,
}

\begin{document}

\section*{Inserting source code}
\begin{lstlisting}
#include<stdio.h>
#include<iostream>
// A comment
int main(void)
{
    printf("Hello World\n");
    return 0;
}
\end{lstlisting}

\end{document}

compile with

htlatex filename hello

the result is following:

enter image description here

  • Your solution works fine, but it's brittle. If I replace just the keywordstyle to read "keywordstyle=\color{red}\ttfamily\bfseries,", both the basicstyle and the keywordstyle are rendered in blue, whether or not I also modify hello.cfg to have .ectt-1000 for the keywordstyle. Could you clarify what is happening, or how to get all typewriter (or any constant-width) font? – Calaf Feb 2 '15 at 22:48

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.