4

I recently asked a similar question about how to change the font in a table produced by an external program. The answer I endorsed solved the problem nicely, but the solution was not intuitive to me given my limited LaTeX knowledge.

I'm faced with a similar challenge of needing to redefine the font of a longtable to monospaced. This table is also produced by another program, so I'm looking for a way to redefine longtable in the preamble rather than just wrap a particular table in a new font.

\documentclass{article}
\usepackage{longtable}
\usepackage{booktabs}


\begin{document}

\begin{longtable}[]{@{}rrrl@{}}
\caption{My title}\tabularnewline
\toprule
v1 & v2 & v3 & v4\tabularnewline
\midrule
\endfirsthead
\toprule
v1 & v2 & v3 & v4\tabularnewline
\midrule
\endhead
1 & 0 & 0 & 0.87\tabularnewline
1 & 0 & 1 & 0.89\tabularnewline
1 & 1 & 0 & 0.87\tabularnewline
\bottomrule
\end{longtable}

\end{document}

2 Answers 2

5

With package etoolbox you can use

\AtBeginEnvironment{longtable}{\ttfamily}

To set the font of the caption load package caption with the settings from your other question.

\documentclass{article}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{etoolbox}
\usepackage[format=plain,
  labelformat=simple,
  font={small,sf,bf},
  indention=0cm,
  labelsep=period,
  justification=centering,
  singlelinecheck=true,
  tableposition=top,
  figureposition=bottom]{caption}

\AtBeginEnvironment{longtable}{\ttfamily}

\usepackage{lipsum}% dummy text
\begin{document}
\begin{longtable}[]{@{}rrrl@{}}
\caption{My title}\tabularnewline
\toprule
v1 & v2 & v3 & v4\tabularnewline
\midrule
\endfirsthead
\toprule
v1 & v2 & v3 & v4\tabularnewline
\midrule
\endhead
1 & 0 & 0 & 0.87\tabularnewline
1 & 0 & 1 & 0.89\tabularnewline
1 & 1 & 0 & 0.87\tabularnewline
\bottomrule
\end{longtable}
\lipsum[1]
\end{document}

enter image description here

1
  • awesome, @esdd. that's much easier than everything i was trying.
    – Eric Green
    Apr 26, 2017 at 12:23
2

You don't say what you tried, but a single \ttfamily would make the table monospace. Using the caption package is a good way to customise captions, but for a one-off use you can simply put \normalfont into the caption resulting in

enter image description here

\documentclass{article}
\usepackage{longtable}
\usepackage{booktabs}


\begin{document}

\ttfamily
\begin{longtable}[]{@{}rrrl@{}}
\caption{\normalfont My title}\tabularnewline
\toprule
v1 & v2 & v3 & v4\tabularnewline
\midrule
\endfirsthead
\toprule
v1 & v2 & v3 & v4\tabularnewline
\midrule
\endhead
1 & 0 & 0 & 0.87\tabularnewline
1 & 0 & 1 & 0.89\tabularnewline
1 & 1 & 0 & 0.87\tabularnewline
\bottomrule
\end{longtable}

\end{document}
1
  • thanks, @DavidCarlisle. due to how i am generating the table and using it in my document, i needed a preamble solution rather than a \ttfamily before the individual table. but it's a good reminder for someone with a different use case!
    – Eric Green
    Apr 26, 2017 at 14:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .