
Я написал простой код для подсветки синтаксиса, когда я цитирую C++ в latex. Однако, когда код напрямую импортируется из исходного файла, заголовки становятся синими, а не зелеными, и это единственное различие между кодами.
1) Вывод из кода C++, напрямую запрограммированный
2) Вывод из кода C++ в файле, импортированном с помощью:
\lstinputlisting[language=C++, firstline=1, lastline=10]{Hello.cpp}
Как видите, заголовки теперь синие!
Вот код латекса, который я использовал для этого:
\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}
решение1
Это сработает, если вы укажете точно такие же настройки, т.е. none:
\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}