6

I'd like to put some lines in the preamble to change the font of all tabulars in the document. I know that I can easily define my own environment »mytabular«. But for different reasons I need to change tabular. I thought about simply adding (see MWE below) code to the macro, unfortunately without success.

\documentclass{article}

    \makeatletter
    \g@addto@macro\tabular\fontfamily{phv}\selectfont
    \makeatother

\begin{document}

This is some text.

\bigskip

\begin{tabular}{lr}
  Hello! & 1234 \\
\end{tabular}
\end{document}

Those tabulars will be used outside floats, so it does not help to change something in the table environment.

  • have you looked at the etoolbox? – cmhughes Jan 5 '14 at 17:35
  • @cmhughes No. Unfortunately I don't have enough knowledge about TeX and its primitives. – Keks Dose Jan 5 '14 at 17:38
5
\documentclass{article}
\let\Tabular\tabular
\let\endTabular\endtabular
\renewcommand\tabular{\sffamily\Tabular}
\renewcommand\endtabular{\endTabular\normalfont}

\begin{document}
This is some text.

\bigskip
\begin{tabular}{lr}
  Hello! & 1234 
\end{tabular}

foo
\end{document}
10

The etoolbox can help here; in particular, the command AtBeginEnvironment can be used

\AtBeginEnvironment{tabular}{\fontfamily{phv}\selectfont}

Here's the output, and a complete MWE to play with:

screenshot

\documentclass{article}
\usepackage{etoolbox}

\AtBeginEnvironment{tabular}{\fontfamily{phv}\selectfont}

\begin{document}

This is some text.

\begin{tabular}{lr}
  Hello! & 1234 \\
\end{tabular}
\end{document}
  • 1
    +1 for introducing \AtBeginEnvironment. – kiss my armpit Jan 5 '14 at 17:47
  • 1
    Or \preto{\tabular}{\fontfamily{phv}\selectfont}, but one must know the definition of tabular for realizing this is possible. – egreg Jan 5 '14 at 19:04
  • Very useful solution both in answer and comment thank you, much favoured over chosen answer as much more user friendly. – Fiztban Jan 30 '15 at 1:57

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.