添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. Visit Stack Exchange

TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.

Sign up to join this community

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I will setup my document to the Sans Serif Font Helvet and additionally all source codes should display with Dejavu Sans Mono and grey background color (on \lstinline{} too). How can I setup listings correctly to do so?

\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}

Using colorbox and a new command I get the inline listing with a background:

\newcommand\code[1]{\colorbox{lstgrey}{{\lstinline`#1`}}}
An example listing shows the \code{main()}:
\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}

Helvetica for main body and Dejavu Sans Mono for listings, right? OK, observe this line basicstyle=\footnotesize\ttfamily, with that \ttfamily at the end. In your post you used the \sf which was the reason for not getting the Dejavu Sans Mono but still getting Dejavu Sans for every thing. You also issued the command \renewcommand{\familydefault}{\sfdefault} which confirmed this.

\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}
                @Alex44  - The helvet package loads only the sans serif font known as Helvetica and it must be activated using, e.g., \renewcommand{\sfdefault}{phv} to be used for the sans serif parts of your text. It doesn't have a typewriter font, however (see this link). The dejavu, on the other hand, has all font shapes (compare this).
– AboAmmar
                Mar 6, 2016 at 13:00
                @AboAmmar This means that this way don't work with other fonts whish provide typewriter fonts too (instead of helvet).
– Alex44
                Mar 6, 2016 at 13:09
                @Alex44  - If both fonts have a typewriter shape, then the one loaded last will be selected. To see this, try to replace helvet with bookman, you will find courier is selected for ttfamily
– AboAmmar
                Mar 6, 2016 at 14:06