LaTeX: Listados de código cómodos y resultones con listings
Ingredientes
- Paquete LaTeX “listings”, en el paquete Debian
texlive-latex-recommended
Uso básico
Este es un ejemplo mínimo de uso del paquete:
\documentclass{article}
\usepackage{listings}
\begin{document}
\begin{lstlisting}[language=C]
#include <stdio.h>
int main(int argc, char* argv[]) {
puts("Hola mundo!");
}
\end{lstlisting}
\end{document}
Y lo que consigues es algo así:
Más bonito
Pero claro, aparte de hacer resaltado de sintaxis, con la configuración adecuada listings
puede hacer mucho más:
Un ejemplo más sofisticado retocando la configuración del entorno:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[spanish]{babel}
\usepackage{times}
\usepackage{color}
\definecolor{gray97}{gray}{.97}
\definecolor{gray75}{gray}{.75}
\definecolor{gray45}{gray}{.45}
\usepackage{listings}
\lstset{ frame=Ltb,
framerule=0pt,
aboveskip=0.5cm,
framextopmargin=3pt,
framexbottommargin=3pt,
framexleftmargin=0.4cm,
framesep=0pt,
rulesep=.4pt,
backgroundcolor=\color{gray97},
rulesepcolor=\color{black},
%
stringstyle=\ttfamily,
showstringspaces = false,
basicstyle=\small\ttfamily,
commentstyle=\color{gray45},
keywordstyle=\bfseries,
%
numbers=left,
numbersep=15pt,
numberstyle=\tiny,
numberfirstline = false,
breaklines=true,
}
% minimizar fragmentado de listados
\lstnewenvironment{listing}[1][]
{\lstset{#1}\pagebreak[0]}{\pagebreak[0]}
\lstdefinestyle{consola}
{basicstyle=\scriptsize\bf\ttfamily,
backgroundcolor=\color{gray75},
}
\lstdefinestyle{C}
{language=C,
}
\begin{document}
\noindent
Escribe el siguiente código en un fichero llamado \texttt{hello.c}:
\begin{lstlisting}[style=C]
#include <stdio.h>
int main(int argc, char* argv[]) {
puts("Hola mundo!");
}
\end{lstlisting}
\noindent
Ahora compila usando \texttt{gcc}:
\begin{listing}[style=consola, numbers=none]
$ gcc -o hello hello.c
\end{listing}
\end{document}
Y te queda algo así de apañao:
Referencias
[ show comments ]
blog comments powered by Disqus