
Escribí un código simple para resaltar la sintaxis cuando cito algo de C++ en látex. Sin embargo, cuando el código se importa directamente desde un archivo fuente, los encabezados están en azul en lugar de verde, siendo esta la única diferencia entre códigos.
1) Salida de un código C++ directamente codificado
2) Salida de un código C++ en un archivo, importado con:
\lstinputlisting[language=C++, firstline=1, lastline=10]{Hello.cpp}
Como puedes ver, ¡los encabezados ahora son azules!
Aquí está el código de látex que utilicé para hacer esto:
\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}
Respuesta1
Funciona si especifica exactamente la misma configuración, es decir, ninguna:
\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}