
Estoy intentando incluir un código C++ en mi documento de látex. Sin embargo, lo que he probado no funciona. Todo lo demás funciona bien, pero no veo que C++ aparezca como un código C++. Simplemente está escrito en el mismo formato que el texto "Partículas alfa ..."
Aquí está el MWE: El tex es:
\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}
Resultado Esperado:
Respuesta1
Desea utilizar una familia de fuentes diferente para el listado. Dado que las palabras clave están en negrita en el estilo, las fuentes estándar de Computer Modern Typewriter no son adecuadas porque no están en negrita.
Una buena familia de fuentes monoespaciadas que distingue bien entre series medianas y negrita es 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}
Tenga en cuenta que para una fórmula en línea use $\alpha$
o \(\alpha\)
, en lugar de \[\alpha\]
que centraría la fórmula.
Eliminé las citas que son irrelevantes para este tema.
Respuesta2
Si no puede ponerse a listings
trabajar, existe el verbatimbox
paquete si no necesita resaltar palabras clave.
Aquí lo muestro listings
y verbatimbox
para comparar. Al utilizar el verbnobox
entorno, se pueden obtener saltos de página en medio del listado.
\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}