4

I am writing my thesis using LaTeX, and a would like to add some code in it. I tried so far the listings by I am not satisfied with the result. Is there a way to add a pre-formated code in HTML into LaTeX?

I want to take my code from Kate as a pre-formated text in HTML and put it into my thesis text. Is that possible?

For example I have the following code in C:

struct cpu_info {
    long unsigned utime, ntime, stime, itime;
    long unsigned iowtime, irqtime, sirqtime;
};

I copy from Kate that code as HTML. The contents of the clipboard are

<pre style='color:#141312;background-color:#ffffff;'>
<b>struct</b> cpu_info {
    <span style='color:#0057ae;'>long</span> <span style='color:#0057ae;'>unsigned</span> utime, ntime, stime, itime;
    <span style='color:#0057ae;'>long</span> <span style='color:#0057ae;'>unsigned</span> iowtime, irqtime, sirqtime;
};</pre>

I want to insert this code in Latex in order to produce the formatted text of the above HTML code without using listings if it is possible.

My code is written in C so if anyone has a good lstset for C, that would be also great for me.

I use \documentclass[a4paper,10pt]{article}.

Peter Grill
  • 223,288
Manos
  • 143
  • maybe try the verbatim environment... – phimuemue Oct 24 '11 at 20:50
  • In which way does the output produced by listing not satisfy you? – Paŭlo Ebermann Oct 24 '11 at 21:27
  • In order to achieve the same result with an editor formatting, it could take me hours to do, as it is the first time that I am using latex. I managed to make an lstset put its output is very poor. So I asked if there is out there any "quick" solution. –  Oct 24 '11 at 21:42
  • 1
    XML/HTML isn't that well supported by listings. See XML syntax highlighting, which might be a duplicate. There is also the minted package which uses Pygments, a Python syntax highlighter, which might have better XML support. – Martin Scharrer Oct 24 '11 at 22:27
  • Also, because your question got migrated from stackoverflow.com to us, please make sure you have a registered account on both sites so that they can be associated to each other. This is required so that you gain ownership of this question on this site. – Martin Scharrer Oct 24 '11 at 22:31
  • Welcome to TeX.SE.

    It is always best to compose a MWE that illustrates the problem including the \documentclass so that those trying to help don't have to recreate it.

    – Peter Grill Oct 24 '11 at 22:36
  • Ok, hello from here and thanks for the replies. @MartinScharrer i edited my question to be more specific. I do not try to highlight html code, I try to import html code as highlighting. Sorry if that was not clear from the beginning. – Manos Oct 24 '11 at 23:03

1 Answers1

6

If possible, I would just use the C code directly. Here is an example:

enter image description here

\documentclass[a4paper,10pt]{article}
\usepackage{xcolor}
\usepackage{listings}
\begin{document}
\lstset{
    language=C,
    basicstyle=\small\sffamily,
    frame=tblr,
    backgroundcolor=\color{yellow!20},
    numbers=left,
    xleftmargin=5.0ex,
    numberstyle=\tiny,
    numbersep=4pt,
    stepnumber=2,
    %showstringspaces=false,
    keywordstyle=\color{blue}\bfseries
    }

\lstset{emph={%  Adjust any special keywords
    printf%
    },emphstyle={\color{red}\bfseries}%
}%

\begin{lstlisting}
/* Hello World program */

#include<stdio.h>

struct cpu_info {
    long unsigned utime, ntime, stime, itime;
    long unsigned iowtime, irqtime, sirqtime;
};

main()
{
    printf("Hello World");
}\end{lstlisting}
\end{document}
Peter Grill
  • 223,288
  • Closer to the original Kate formatting (but I'm not sure why it leaves 'struct' uncolored, but colors 'unsigned' and 'long', since they're all keywords):

    `\definecolor{keyword}{HTML}{0057AE} \definecolor{basic}{HTML}{141312} \definecolor{background}{HTML}{FFFFFF}

    \lstset{language=C, backgroundcolor=\color{background}, basicstyle=\color{basic}\ttfamily, keywordstyle=\color{keyword}\bfseries, }`

    – Mike Renfro Oct 25 '11 at 16:39
  • I wasn't mimicking any formatting, just suggesting that it is better to simply use the C code directly rather than try to format the HTML version of the C code. – Peter Grill Oct 25 '11 at 16:50
  • @pe I ended with your suggestion using C code directly. There was a problem with the code that overfilled the margins of my text, due to many nested code. The solution to that was to minimize the size of tabs. With a little search I found the tabsize parameter, set it to 2 and "Wohoooo" problem is gone. Thanks all for your help. – Manos Oct 25 '11 at 18:00
  • @PeterGrill sure, I just added it in case the original poster got picky about specific colors matching what Kate produced. No doubt it's better to just use listings for C in any case. – Mike Renfro Oct 26 '11 at 01:23
  • This doesn't answer the question of inserting preformatted html :( – Ell Feb 16 '17 at 19:01
  • @Ell: Yeah, I suggested using the C code directly as the op stated "My code is written in C so if anyone has a good lstset for C, that would be also great for me." – Peter Grill Feb 17 '17 at 00:39
  • I suppose. Maybe this question is a duplicate though. Oh well, I've found a way around my issue anyway. – Ell Feb 17 '17 at 00:46
  • @Ell: If you found what you wanted in another answer here, you should add a link to it in a comment so that others may find it. If you came up with the solution it would be helpful if you posted it as an answer here. – Peter Grill Feb 17 '17 at 11:26