segunda-feira, 30 de julho de 2012

Dica: Mostrando código C++, Java, etc no LaTeX

Veja aqui como mostrar códigos C++, Java, etc no LaTeX.
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}





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}


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}


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={},
}
...

Baixe o código completo em codigomultilanguage.tex

Referência: LaTeX syntax highlighting examples
Leia também: Mostrando código ao lado da figura

2 comentários:

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

    ResponderExcluir
  2. Excelentes as dicas. Foram muito úteis!!

    ResponderExcluir