Fonts & Character SetsChanging Font Type Within Tables

Information and discussion about fonts and character sets (e.g. how to use language specific characters)
Post Reply
workerbee
Posts: 43
Joined: Sat Nov 22, 2008 1:53 am

Changing Font Type Within Tables

Post by workerbee »

Hi All,

I was wondering if there was a way to change the Font type within the table environments. For instance, I just wish to change text and data within tables to a Courier New font. Is there a way I can do this with a global command?

Any feedback would be most appreciated, thanks!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
Skazz
Posts: 2
Joined: Wed Dec 09, 2009 7:05 pm

Changing Font Type Within Tables

Post by Skazz »

Hi workerbee,

You could declare a new environment, which changes the font and then calls the standard tabular environment. e.g.

Code: Select all

\documentclass{article}
\usepackage[utf8]{inputenc}

\newenvironment{tttabular}[1]%
{\ttfamily \begin{tabular}{#1}}%
{\end{tabular}}

\begin{document}

\begin{tttabular}{c|c|c|c|c|c|c|}
 the &brown &fox &jumps &over &the &fence\\
 over &the &fence &the &brown &fox &jumps
\end{tttabular}

\end{document}
I hope I could help!
Skazz
workerbee
Posts: 43
Joined: Sat Nov 22, 2008 1:53 am

Re: Changing Font Type Within Tables

Post by workerbee »

Thanks, Skazz! It works wonderfully.

I was also wondering if there was a way to do this without having to define a new environment--maybe a similar way in how we can change line spacing in the itemize environment using the enumitem package.
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

Changing Font Type Within Tables

Post by localghost »

workerbee wrote:[…] I was also wondering if there was a way to do this without having to define a new environment--maybe a similar way in how we can change line spacing in the itemize environment using the enumitem package.
Answering to a very similar request in a German forum, I stumbled upon a very elegant solution by Axel Sommerfeldt (our fellow member sommerfee).

Code: Select all

\documentclass[11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage[font=small,labelfont=bf,tableposition=top]{caption}

\makeatletter
% Axel Sommerfeldt [2007/01/07]
\providecommand*\AtBeginEnvironment[1]{%
  \@ifundefined{#1}%
    {\@latex@error{Environment #1 undefined}\@ehc
     \@gobble}%
    {\@ifundefined{ABE@env@#1}%
       {\expandafter\let\csname ABE@env@#1\expandafter\endcsname
          \csname #1\endcsname
        \expandafter\let\csname ABE@hook@#1\endcsname\@empty
        \@namedef{#1}{\@nameuse{ABE@hook@#1}\@nameuse{ABE@env@#1}}}%
       {}%
     \expandafter\g@addto@macro\csname ABE@hook@#1\endcsname}}
\@onlypreamble\AtBeginEnvironment
\makeatother

\AtBeginEnvironment{tabular}{\ttfamily}

\begin{document}
  \begin{table}[!ht]
    \caption{A Table}\label{tab:table}
    \centering
    \begin{tabular}{cc}\hline
      Table Head & Table Head \\ \hline
      A Value & A Value \\
      A Value & A Value \\
      A Value & A Value \\ \hline
    \end{tabular}
  \end{table}
\end{document}
This is a universal solution since it works with every environment.


Best regards
Thorsten
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
workerbee
Posts: 43
Joined: Sat Nov 22, 2008 1:53 am

Re: Changing Font Type Within Tables

Post by workerbee »

Thanks, Thorsten!
greyshark
Posts: 1
Joined: Mon Oct 28, 2024 8:22 am

Changing Font Type Within Tables

Post by greyshark »

Simpler solution:

Code: Select all

\usepackage{etoolbox}
\AtBeginEnvironment{tabular}{\sffamily} % Changes all tabular environments to sans-serif
ann43587
Posts: 1
Joined: Wed Nov 20, 2024 1:36 pm

Changing Font Type Within Tables

Post by ann43587 »

Hi there,

Yes, you can change the font within tables to Courier New (or any other font) using LaTeX. A simple and global way to achieve this is by redefining the table environment's font. Here’s how you can do it:

\usepackage{array} % Needed for table customizations

% Redefine table font globally
\let\oldtabular\tabular
\let\endoldtabular\endtabular
\renewenvironment{tabular}{\bgroup\ttfamily\oldtabular}{\endoldtabular\egroup}

This approach makes all text inside your tabular environments use \ttfamily, which corresponds to a monospaced font like Courier New. If you want to limit it to certain tables or parts of your document, you can wrap only specific instances of tabular with the custom font commands:

\begin{tabular}{|c|c|}
\hline
\texttt{Example} & \texttt{Text in Courier New} \\
\hline
\end{tabular}

Let me know if you need further assistance or more tailored solutions! :D
Post Reply