2010-12-14 32 views
7

¿Alguien me puede recomendar una buena plantilla para incluir el código fuente C con la numeración de línea en látex? Por ejemplo, tomando el programa Hola mundo clásico, me gustaría hacer que se vea como sigue:C Código fuente en documento de látex

(1) /* Hello World program */ 
(2) 
(3) #include<stdio.h> 
(4) 
(5) main() 
(6) { 
(7) printf("Hello World"); 
(8) } 

typicall, siempre utiliza el entorno verbatim, pero estoy preguntando si hay una manera mejor y más agradable de hacer ese.

Gracias tanto Richard

+1

vea tex.stackexchange.com también vea http://stackoverflow.com/questions/741985/latex-source-code-listing-like-in-professional-books – deinst

+0

Probablemente se puede obtener una mejor respuesta [TeX y LaTeX stackexchange] (http://tex.stackexchange.com/). –

Respuesta

9

Es posible que desee echar un vistazo a listings package. Es muy flexible y fácil de usar.

0

CWEB tenía un buen formateador en C.

9

Como han dicho otros, el paquete listings probablemente hacer lo que desee con algo como lo siguiente:

\lstset{ 
    language=C,    % choose the language of the code 
    numbers=left,     % where to put the line-numbers 
    stepnumber=1,     % the step between two line-numbers.   
    numbersep=5pt,     % how far the line-numbers are from the code 
    backgroundcolor=\color{white}, % choose the background color. You must add \usepackage{color} 
    showspaces=false,    % show spaces adding particular underscores 
    showstringspaces=false,   % underline spaces within strings 
    showtabs=false,     % show tabs within strings adding particular underscores 
    tabsize=2,      % sets default tabsize to 2 spaces 
    captionpos=b,     % sets the caption-position to bottom 
    breaklines=true,    % sets automatic line breaking 
    breakatwhitespace=true,   % sets if automatic breaks should only happen at whitespace 
    title=\lstname,     % show the filename of files included with \lstinputlisting; 
} 

\lstinputlisting{HelloWorld.c} 

Una alternativa más potente sería utilizar el paquete minted, aunque esto va a hacer mucho más de lo lo que está pidiendo actualmente, ya que usa/requiere que los pygments se instalen en su sistema para que pueda tokenizar completamente el código que le da.

0

Utilice el paquete lgrind para látex. Convierte su código en un archivo .tex

Cuestiones relacionadas