LaTeX forum ⇒ Fonts & Character SetsChanging Font Type Within Tables

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

Changing Font Type Within Tables

Postby workerbee » Wed Dec 09, 2009 10:59 pm

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
Skazz
Posts: 2
Joined: Wed Dec 09, 2009 7:05 pm

Changing Font Type Within Tables

Postby Skazz » Thu Dec 10, 2009 12:24 am

Hi workerbee,

You could declare a new environment, which changes the font and then calls the standard tabular environment. e.g.
\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

Postby workerbee » Fri Dec 11, 2009 12:40 am

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

Postby localghost » Sat Dec 12, 2009 12:16 pm

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).
\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
LaTeX Community Moderator
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ 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

Postby workerbee » Tue Dec 15, 2009 12:36 am

Thanks, Thorsten!

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

Changing Font Type Within Tables

Postby greyshark » Mon Oct 28, 2024 8:24 am

Simpler solution:
\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

Postby ann43587 » Wed Nov 20, 2024 1:43 pm

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


Return to “Fonts & Character Sets”

Who is online

Users browsing this forum: No registered users and 4 guests