从源导入时引用的代码会改变颜色

从源导入时引用的代码会改变颜色

我写了一个简单的代码,用于在 latex 中引用一些 C++ 时突出显示语法。但是,当直接从源文件导入代码时,标题是蓝色而不是绿色,这是代码之间的唯一区别。

1)直接硬编码的 C++ 代码输出

硬编码 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

如果您指定完全相同的设置(即无设置),它就会起作用:

\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}

绿色语法

相关内容