4

I would like to change to a font with sans serif (for example Helvetica or computer modern sans serif). The following code does not change the font:

\documentclass{article}

\usepackage[english,ngerman]{babel}
\usepackage[T1]{fontenc}
\newcommand{\changefont}[3]{
\fontfamily{#1} \fontseries{#2} \fontshape{#3} \selectfont}

\changefont{phv}{m}{n}

\begin{document}
THIS IS A TEST. 
this is a test.\\

\begin{tabular}{lr} \hline 
ABCD & 1234       \\
abcd & 1234,567   \\
UVWXY & 1,234,567 \\
uvwxy & 1,234,567 \\ \hline
\end{tabular}

\end{document}

I get a pdf-file with serif. There are no errors and warnings. I'm using Windows 7, MikTex, TeXnicCenter. Does anyone have an idea why this does not work?

  • 2
    Place the \changefont after \begin{document} – Steven B. Segletes Feb 25 '14 at 17:16
  • 1
    When processing \begin{document}, LaTeX issues \normalfont; you should place \changefont after it. But there are better ways to get a document in a sans serif font. – egreg Feb 25 '14 at 17:16
  • 1
    Your \changefont command will introduce 4 word-spaces at every point it is used, I suspect that you do not want that (you could use the standard \usefont command which is almost the same except takes an additional encoding argument, and doesn't have white space issues) – David Carlisle Feb 25 '14 at 17:20
7

Your method doesn't work because \begin{document} also issues \normalfont, so nullifying your font choice. You could place the command after \begin{document} or say

\newcommand{\changefont}[3]{%
  \fontfamily{#1}\fontseries{#2}\fontshape{#3}\selectfont}
\AtBeginDocument{\changefont{phv}{m}{n}}

but this is an unneeded complication and

\AtBeginDocument{\usefont{\encodingdefault}{phv}{m}{n}}

would do exactly the same. However, I wouldn't recommend such an approach.

If you want your document to be in sans serif font, the best approach is to change the meaning of \familydefault to \sfdefault.

Then, instead of specifying the font family with a low level command like your \changefont, it's better to select the font with a higher level method:

\documentclass{article}

\usepackage[english,ngerman]{babel}
\usepackage[T1]{fontenc}

\usepackage[scale=.9]{tgheros} % or helvet
\renewcommand{\familydefault}{\sfdefault}

\begin{document}
THIS IS A TEST.
this is a test.

\bigskip

\begin{tabular}{lr} \hline
ABCD & 1234       \\
abcd & 1234,567   \\
UVWXY & 1,234,567 \\
uvwxy & 1,234,567 \\ \hline
\end{tabular}

\end{document}

Don't use \\ followed a blank line in order to have vertical space.

enter image description here

  • Thanks a lot! For you some lines for me a huge step for understanding latex-concept. – giordano Feb 27 '14 at 14:56

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.