O pacote listings mostra os códigos de linguagem. Além disso, precisamos de algumas configurações. Veja o código completo.
C++
\documentclass[a4paper]{article}
\usepackage{xcolor}
% Definindo novas cores
\definecolor{verde}{rgb}{0,0.5,0}
% Configurando layout para mostrar codigos C++
\usepackage{listings}
\lstset{
language=C++ ,
basicstyle=\ttfamily\small,
keywordstyle=\color{blue},
stringstyle=\color{verde},
commentstyle=\color{red},
extendedchars=true,
showspaces=false,
showstringspaces=false,
numbers=left,
numberstyle=\tiny,
breaklines=true,
backgroundcolor=\color{green!10},
breakautoindent=true,
captionpos=b,
xleftmargin=0pt,
}
\pagestyle{empty}
\begin{document}
\begin{lstlisting}
#include
using namespace std;
int main()
{
/* comentario */
int n, i, a = 0, b = 1, F;
cout << "Digite o numero de termos da sequencia de Fibonacci: ";
cin >> n;
cout << a << " " << b << " ";
for (i = 0; i < n - 2; i++) {
F = a + b ;
cout << F << " ";
a = b;
b = F;
} cout << endl; return 0;
}\end{lstlisting}
\end{document}
\usepackage{xcolor}
\definecolor{verde}{rgb}{0,0.5,0}
\usepackage{listings}
\lstset{
language=
basicstyle=\ttfamily\small,
keywordstyle=\color{blue},
stringstyle=\color{verde},
commentstyle=\color{red},
extendedchars=true,
showspaces=false,
showstringspaces=false,
numbers=left,
numberstyle=\tiny,
breaklines=true,
backgroundcolor=\color{green!10},
breakautoindent=true,
captionpos=b,
xleftmargin=0pt,
}
\pagestyle{empty}
#include
using namespace std;
int main()
{
/* comentario */
int n, i, a = 0, b = 1, F;
cout << "Digite o numero de termos da sequencia de Fibonacci: ";
cout << a << " " << b << " ";
cout << F << " ";
a = b;
b = F;
} cout << endl; return 0;
}
Java
\documentclass[a4paper]{article}
\usepackage{xcolor}
% Definindo novas cores
\definecolor{verde}{rgb}{0.25,0.5,0.35}
\definecolor{jpurple}{rgb}{0.5,0,0.35}
% Configurando layout para mostrar codigos Java
\usepackage{listings}
\lstset{
language=Java ,
basicstyle=\ttfamily\small,
keywordstyle=\color{jpurple}\bfseries,
stringstyle=\color{red},
commentstyle=\color{verde},
morecomment=[s][\color{blue}]{/**}{*/},
extendedchars=true,
showspaces=false,
showstringspaces=false,
numbers=left,
numberstyle=\tiny,
breaklines=true,
backgroundcolor=\color{cyan!10},
breakautoindent=true,
captionpos=b,
xleftmargin=0pt,
tabsize=4
}
\pagestyle{empty}
\begin{document}
\begin{lstlisting}
/**
* comentario
*/
public class HelloWorldApp {
public static void main (String argv[])
{
// Comentario
System.out.println("Hello World!");
}
}
\end{lstlisting}
\end{document}
\usepackage{xcolor}
\definecolor{verde}{rgb}{0.25,0.5,0.35}
\definecolor{jpurple}{rgb}{0.5,0,0.35}
\usepackage{listings}
\lstset{
language=
basicstyle=\ttfamily\small,
keywordstyle=\color{jpurple}\bfseries,
stringstyle=\color{red},
commentstyle=\color{verde},
morecomment=[s][\color{blue}]{/**}{*/},
extendedchars=true,
showspaces=false,
showstringspaces=false,
numbers=left,
numberstyle=\tiny,
breaklines=true,
backgroundcolor=\color{cyan!10},
breakautoindent=true,
captionpos=b,
xleftmargin=0pt,
tabsize=4
}
\pagestyle{empty}
/**
* comentario
*/
public class HelloWorldApp {
public static void main (String argv[])
{
// Comentario
System.out.println("Hello World!");
}
}
LaTeX
\documentclass[a4paper]{article}
\usepackage{xcolor,lipsum}
\usepackage{listings,showexpl}
\lstset{explpreset={
language=[LaTeX]TeX ,
basicstyle=\ttfamily\small,
identifierstyle=\color{black},
keywordstyle=\color{blue},
commentstyle=\color{red},
extendedchars=true,
showspaces=false,
showstringspaces=false,
numbers=none,
%numberstyle=\tiny,
breaklines=true,
backgroundcolor=\color{yellow!20},
breakautoindent=true,
captionpos=b,
xleftmargin=0pt,
frame=none,%%frame=single
rframe={}
}
}
\pagestyle{empty}
\begin{document}
\begin{LTXexample}
\documentclass{article}
\usepackage{xcolor,lipsum}
\usepackage{listings,showexpl}
\lstset{explpreset={
language=[LaTeX]TeX,
basicstyle=\ttfamily\small,
identifierstyle=\color{black},
keywordstyle=\color{blue},
commentstyle=\color{red},
extendedchars=true,
showspaces=false,
showstringspaces=false,
numbers=none,
%numberstyle=\tiny,
breaklines=true,
backgroundcolor=\color{yellow!20},
breakautoindent=true,
captionpos=b,
xleftmargin=0pt,
frame=none,
rframe={}
}
}
\begin{document}
% comentario
\lipsum[1-2]
\end{document}
\end{LTXexample}
\end{document}
\end{document}
\usepackage{xcolor,lipsum}
\usepackage{listings,showexpl}
\lstset{explpreset={
language=
basicstyle=\ttfamily\small,
identifierstyle=\color{black},
keywordstyle=\color{blue},
commentstyle=\color{red},
extendedchars=true,
showspaces=false,
showstringspaces=false,
numbers=none,
%numberstyle=\tiny,
breaklines=true,
backgroundcolor=\color{yellow!20},
breakautoindent=true,
captionpos=b,
xleftmargin=0pt,
frame=none,
rframe={}
}
}
\pagestyle{empty}
\documentclass{article}
\usepackage{xcolor,lipsum}
\usepackage{listings,showexpl}
\lstset{explpreset={
language=[LaTeX]TeX,
basicstyle=\ttfamily\small,
identifierstyle=\color{black},
keywordstyle=\color{blue},
commentstyle=\color{red},
extendedchars=true,
showspaces=false,
showstringspaces=false,
numbers=none,
%numberstyle=\tiny,
breaklines=true,
backgroundcolor=\color{yellow!20},
breakautoindent=true,
captionpos=b,
xleftmargin=0pt,
frame=none,
rframe={}
}
}
\begin{document}
\lipsum[1-2]
\end{document}
\end{document}
Várias linguagens no mesmo arquivo
Veja a seguir como ter várias configurações de linguagem no mesmo arquivo.Precisamos do comando \lstdefinestyle{}.
...
\lstdefinestyle{LaTeX}{
language={[LaTeX]TeX},
basicstyle=\ttfamily\small,
identifierstyle=\color{black},
keywordstyle=\color{blue},
commentstyle=\color{red},
extendedchars=true,
showspaces=false,
showstringspaces=false,
numbers=none,
breaklines=true,
backgroundcolor=\color{yellow!20},
breakautoindent=true,
captionpos=b,
xleftmargin=0pt,
frame=none,
rframe={},
}
...
\lstdefinestyle{LaTeX}{
language={[LaTeX]TeX},
basicstyle=\ttfamily\small,
identifierstyle=\color{black},
keywordstyle=\color{blue},
commentstyle=\color{red},
extendedchars=true,
showspaces=false,
showstringspaces=false,
numbers=none,
breaklines=true,
backgroundcolor=\color{yellow!20},
breakautoindent=true,
captionpos=b,
xleftmargin=0pt,
frame=none,
rframe={},
}
...
Baixe o código completo em codigomultilanguage.tex
Referência: LaTeX syntax highlighting examples
Leia também: Mostrando código ao lado da figura
Outra dica é o ótimo pacote minted [http://www.ctan.org/tex-archive/macros/latex/contrib/minted/ , http://code.google.com/p/minted/] que tem um ótimo suporte a UTF-8 e a várias linguagens.
ResponderExcluirExcelentes as dicas. Foram muito úteis!!
ResponderExcluir