
當我在 Latex 中引用一些 C++ 時,我編寫了一個簡單的程式碼來突出顯示語法。但是,當直接從原始檔案匯入程式碼時,標題為藍色而不是綠色,這是程式碼之間的唯一區別。
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
如果您指定完全相同的設定(即沒有指定),它會起作用:
\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}