
라텍스에서 일부 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}