Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}.

share|improve this question

migrated from stackoverflow.com Oct 24 '11 at 22:21

This question came from our site for professional and enthusiast programmers.

    
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. –  Manos 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
show 2 more comments

1 Answer

up vote 6 down vote accepted

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}
share|improve this answer
    
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 1:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.