라텍스 문서에 C++ 코드 포함

라텍스 문서에 C++ 코드 포함

내 라텍스 문서에 C++ 코드를 포함하려고 합니다. 그러나 내가 시도한 것은 작동하지 않습니다. 다른 모든 것은 잘 작동하지만 C++가 C++ 코드로 표시되지 않습니다. 그냥 "알파입자.." 텍스트와 같은 형식으로 쓰여 있을 뿐입니다.

MWE는 다음과 같습니다. tex는 다음과 같습니다.

\documentclass{article}  
 \usepackage{listings}
\usepackage{xcolor}
\lstset { 
language=C++,
backgroundcolor=\color{black!5}, % set backgroundcolor
basicstyle=\footnotesize,% basic font setting
}

\begin{document}  
Alpha particles \cite{wikip} (named \cite{Comp} after and denoted by the first letter     in the
Greek alphabet,\[\alpha\]) consist of two protons and two neutrons bound
together.
This means that an particle is a helium nucleus. 

\begin{lstlisting}
int size =1;
*ptr = Mole:getMole(size + 1);
\end{lstlisting}

\bibliographystyle{plain}
\bibliography{BibName}

\end{document}

예상 결과:

여기에 이미지 설명을 입력하세요

답변1

목록에 다른 글꼴 모음을 사용하려고 합니다. 키워드가 굵은 글씨체이기 때문에 표준 Computer Modern Typewriter 글꼴은 굵은 글씨체가 없기 때문에 적합하지 않습니다.

중간 시리즈와 볼드체를 잘 구별하는 좋은 고정 폭 글꼴 모음은 BeraMono입니다.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage{xcolor}
\usepackage[scaled=.85]{beramono}

\lstset{
  language=C++,
  backgroundcolor=\color{black!5}, % set backgroundcolor
  basicstyle=\footnotesize\ttfamily,% basic font setting
  columns=fullflexible,
}

\begin{document}

Alpha particles (named after and denoted by the first letter in the Greek alphabet,
$\alpha$) consist of two protons and two neutrons bound together. This means that an
particle is a helium nucleus.

\begin{lstlisting}
int size =1;
*ptr = Mole:getMole(size + 1);
\end{lstlisting}

\end{document}

인라인 수식의 경우 수식을 중앙에 배치하는 대신 $\alpha$또는 을 사용합니다.\(\alpha\)\[\alpha\]

이 주제와 관련 없는 인용을 삭제했습니다.

여기에 이미지 설명을 입력하세요

답변2

작업을 시작할 수 없는 경우 키워드 강조가 필요하지 않은 경우 패키지가 listings있습니다 .verbatimbox

listings여기에 비교 를 위해 표시합니다 verbatimbox. 환경 을 사용하면 verbnobox목록 중간에 페이지 나누기를 얻을 수 있습니다.

\documentclass{article}
\usepackage{xcolor}
% LISTINGS PREP
\usepackage{listings}
\lstset { 
language=C++,
backgroundcolor=\color{black!5}, % set backgroundcolor
basicstyle=\footnotesize,% basic font setting
}
% VERBATIMBOX PREP
\usepackage{verbatimbox}
\def\codesize{\footnotesize}
\newsavebox\thecolorfield
\newcommand\setcolorfield[1][blue!9!gray!8]{%
  \savebox{\thecolorfield}{\codesize%
    \makebox[0pt][l]{\textcolor{#1}{%
    \rule[-\dp\strutbox]{\textwidth}{\dimexpr\ht\strutbox+\dp\strutbox}}}}%
}
\def\colorfield{\usebox{\thecolorfield}}
\setcolorfield
%
\begin{document}  
WITH LISTINGS:
\begin{lstlisting}
int size =1;
*ptr = Mole:getMole(size + 1);
\end{lstlisting}

WITH VERBATIMBOX (ttfamily):
\begin{verbnobox}[\colorfield\ttfamily\codesize]
int size =1;
*ptr = Mole:getMole(size + 1);
\end{verbnobox}

\setcolorfield[green!8]
WITH VERBATIMBOX (bold-rmfamily):
\begin{verbnobox}[\colorfield\rmfamily\bfseries\codesize]
int size =1;
*ptr = Mole:getMole(size + 1);
\end{verbnobox}
\end{document}

여기에 이미지 설명을 입력하세요

관련 정보