
Escrevi um código simples para destacar a sintaxe quando cito algum C++ em látex. Porém, quando o código é importado diretamente de um arquivo fonte, os cabeçalhos ficam em azul em vez de verde, sendo esta a única diferença entre os códigos.
1) Saída de um código C++ diretamente codificado
2) Saída de um código C++ em um arquivo, importado com:
\lstinputlisting[language=C++, firstline=1, lastline=10]{Hello.cpp}
Como você pode ver, os cabeçalhos agora estão azuis!
Aqui está o código de látex que usei para fazer isso:
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{fullpage}
\newcommand{\grayScale}{0.95} % Can change the gray level here
\definecolor{codeBackground}{rgb}{\grayScale ,\grayScale ,\grayScale}
\definecolor{forestGreen}{rgb}{0.13,0.55,0.13}
\begin{document}
% Using typewriter font: \ttfamily inside \lstset
\lstset{
language=C++,
backgroundcolor=\color{codeBackground},
tabsize=4,
showstringspaces=false,
showtabs=false,
showspaces=false,
basicstyle=\ttfamily,
identifierstyle=\ttfamily,
keywordstyle=\color{blue},
stringstyle=\color{red},
commentstyle=\color{gray},
numberstyle=\color{magenta},
morecomment=[l][\color{forestGreen}]{\#}
}
\lstset{literate=% Colors the digits
*{0}{{{\color{red!20!violet}0}}}1
{1}{{{\color{red!20!violet}1}}}1
{2}{{{\color{red!20!violet}2}}}1
{3}{{{\color{red!20!violet}3}}}1
{4}{{{\color{red!20!violet}4}}}1
{5}{{{\color{red!20!violet}5}}}1
{6}{{{\color{red!20!violet}6}}}1
{7}{{{\color{red!20!violet}7}}}1
{8}{{{\color{red!20!violet}8}}}1
{9}{{{\color{red!20!violet}9}}}1
}
\begin{lstlisting}
% HERE IS MY HARDCODED C++
#include<stdio.h>
#include<iostream>
// Template for C++ quoting
/*
Warning: If line too long, will go outside the color box
*/
int main(void)
{
cout << "Hello World" << endl;
return 0; // Numbers are colored when not in comment nor strings
}
\end{lstlisting}
% FROM A SOURCE FILE
\lstinputlisting[language=C++, firstline=1, lastline=10]{Hello.cpp}
\end{document}
Responder1
Funciona se você especificar exatamente as mesmas configurações, ou seja, nenhuma:
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{fullpage}
\newcommand{\grayScale}{0.95} % Can change the gray level here
\definecolor{codeBackground}{rgb}{\grayScale ,\grayScale ,\grayScale}
\definecolor{forestGreen}{rgb}{0.13,0.55,0.13}
\begin{document}
% Using typewriter font: \ttfamily inside \lstset
\lstset{
language=C++,
backgroundcolor=\color{codeBackground},
tabsize=4,
showstringspaces=false,
showtabs=false,
showspaces=false,
basicstyle=\ttfamily,
identifierstyle=\ttfamily,
keywordstyle=\color{blue},
stringstyle=\color{red},
commentstyle=\color{gray},
numberstyle=\color{magenta},
morecomment=[l][\color{forestGreen}]{\#}
}
\lstset{literate=% Colors the digits
*{0}{{{\color{red!20!violet}0}}}1
{1}{{{\color{red!20!violet}1}}}1
{2}{{{\color{red!20!violet}2}}}1
{3}{{{\color{red!20!violet}3}}}1
{4}{{{\color{red!20!violet}4}}}1
{5}{{{\color{red!20!violet}5}}}1
{6}{{{\color{red!20!violet}6}}}1
{7}{{{\color{red!20!violet}7}}}1
{8}{{{\color{red!20!violet}8}}}1
{9}{{{\color{red!20!violet}9}}}1
}
\begin{lstlisting}
% HERE IS MY HARDCODED C++
#include<stdio.h>
#include<iostream>
// Template for C++ quoting
/*
Warning: If line too long, will go outside the color box
*/
int main(void)
{
cout << "Hello World" << endl;
return 0; // Numbers are colored when not in comment nor strings
}
\end{lstlisting}
% FROM A SOURCE FILE
\lstinputlisting[firstline=1, lastline=12]{Hello.cpp}
\end{document}