使用等宽字体 (Dejavu Sans Mono) 和背景颜色列出

使用等宽字体 (Dejavu Sans Mono) 和背景颜色列出

我将把文档设置为 Sans Serif 字体Helvet,此外所有源代码都应显示Dejavu Sans Mono灰色背景颜色(\lstinline{}也开启)。我该如何listings正确设置才能做到这一点?

\documentclass[idxtotoc,hyperref,openany,ngerman,11pt]{article} 
\usepackage[a4paper,
            inner=30mm, outer=20mm,
            top=25mm, bottom=25mm,
            headheight=15mm, headsep=7mm
            ]{geometry}

\usepackage{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{helvet}
\renewcommand{\familydefault}{\sfdefault}
\fontfamily{phv}\selectfont

\usepackage{amsmath,amsfonts,amssymb,amsthm,cancel,icomma,nicefrac,mathrsfs,
            eurosym,verbatim,environ,ifthen,ifdraft,pdfpages,float,booktabs}
\allowdisplaybreaks[1] 

\usepackage{color}
\definecolor{lstgrey}{rgb}{0.95,0.95,0.95}
\usepackage{dejavu}
\usepackage{listings}
\lstset{language=C,
       backgroundcolor=\color{lstgrey},
       frame=single,
       basicstyle=\footnotesize\sf,
       captionpos=b,
       tabsize=2,
  }

\begin{document}
An example listing shows the \lstinline|main()|:

\begin{lstlisting}[caption=main.c]
int main (void)
{
  return 0;
};
\end{lstlisting}

\end{document}

在此处输入图片描述


使用colorbox新命令我得到了带有背景的内联列表:

\newcommand\code[1]{\colorbox{lstgrey}{{\lstinline`#1`}}}
...
An example listing shows the \code{main()}:

在此处输入图片描述


感谢 AboAmmar 的最终代码:

\documentclass[hyperref,ngerman,11pt]{article} 
\usepackage[a4paper,
            inner=30mm, outer=20mm,
            top=25mm, bottom=25mm,
            headheight=15mm, headsep=7mm
            ]{geometry}

\usepackage{babel}
\usepackage{dejavu}
\usepackage[T1]{fontenc}

\usepackage{helvet}
\renewcommand{\familydefault}{\sfdefault}

\usepackage{amsmath,amsfonts,amssymb,amsthm,cancel,icomma,
            nicefrac,mathrsfs,eurosym,verbatim,environ,
            ifthen,ifdraft,pdfpages,float,booktabs}
\allowdisplaybreaks[1] 

\usepackage{color}
\definecolor{lstgrey}{rgb}{0.95,0.95,0.95}
\usepackage{listings}
\lstset{language=C,
       backgroundcolor=\color{lstgrey},
       frame=single,
       basicstyle=\footnotesize\ttfamily,
       captionpos=b,
       tabsize=2,
  }
\newcommand\code[1]{\colorbox{lstgrey}{{\lstinline`#1`}}}

\begin{document}
An example listing shows the \code{main()}:

\begin{lstlisting}[caption=main.c]
int main (void)
{
  return 0;
};
\end{lstlisting}

\end{document}

在此处输入图片描述

答案1

Helvetica主体和Dejavu Sans Mono列表,对吧?好的,观察末尾的basicstyle=\footnotesize\ttfamily,这一行。在您的帖子中,您使用了,这就是未获取但仍获取所有内容的原因。您还发出了确认这一点的命令。\ttfamily\sfDejavu Sans MonoDejavu Sans\renewcommand{\familydefault}{\sfdefault}

\documentclass[hyperref,ngerman,11pt]{article} 
\usepackage[a4paper,
            inner=30mm, outer=20mm,
            top=25mm, bottom=25mm,
            headheight=15mm, headsep=7mm
            ]{geometry}

\usepackage{babel}
\usepackage{dejavu}
\usepackage[T1]{fontenc}

\usepackage{helvet}
\renewcommand{\familydefault}{\sfdefault}

\usepackage{amsmath,amsfonts,amssymb,amsthm,cancel,icomma,nicefrac,mathrsfs,
            eurosym,verbatim,environ,ifthen,ifdraft,pdfpages,float,booktabs}
\allowdisplaybreaks[1] 

\usepackage{color}
\definecolor{lstgrey}{rgb}{0.95,0.95,0.95}
\usepackage{listings}
\lstset{language=C,
       backgroundcolor=\color{lstgrey},
       frame=single,
       basicstyle=\footnotesize\ttfamily,
       captionpos=b,
       tabsize=2,
  }

\begin{document}
An example listing shows the \lstinline|main()|:

\begin{lstlisting}[caption=main.c]
int main (void)
{
  return 0;
};
\end{lstlisting}

\end{document}

在此处输入图片描述

相关内容