2

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}

enter image description here


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()}:

enter image description here


Final code with thanks to 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}

enter image description here

  • And the question is? – egreg Mar 5 '16 at 20:39
  • How to set-up listings that listing are displayed with Dejavu Sans Mono? – Alex44 Mar 5 '16 at 20:49
2

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}

enter image description here

  • \ttfamily, please – egreg Mar 6 '16 at 11:47
  • Why you can say, thar ttfamily is linked to DejaVu and not to Helvetia? – Alex44 Mar 6 '16 at 12:18
  • 2
    @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 '16 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 '16 at 13:09
  • 1
    @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 '16 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.