# \setcaptionfont in tufte-book does not work

I want to format all my captions (tables, figures, ...) in tufte-book. The built-in \setcaptionfont command leads to surprising results: margin tables are all formatted, captions of normal tables or figures not at all. Am I the only one...? I use Lyx 2.3.

%% LyX 2.3.0 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[ngerman,nohyper]{tufte-book}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\synctex=-1
\usepackage{babel}
\usepackage[unicode=true,pdfusetitle,
bookmarks=true,bookmarksnumbered=false,bookmarksopen=true,bookmarksopenlevel=1,
{hyperref}

\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\setcaptionfont{%
\color{red}
}
\usepackage{lipsum}

\makeatother

\begin{document}
\begin{margintable}
Red table

\caption{Red caption}
\end{margintable}

\lipsum[5]

\begin{figure}
Black text

\caption{Black Caption}
\end{figure}

\end{document}


# The solution to the figure caption

You specified nohyper as your class option but then loaded the hyperref package anyway. Removing the hyperref package solves your problem. Just tell LyX not to load hyperref, and please consider using utf8 for input encoding and specify a language for babel.

Added: If you do want to load hyperref, then just remove the class option nohyper (tufte-book loads hyperref by default).

\documentclass[ngerman,nohyper]{tufte-book}
\usepackage[utf8]{inputenc}% utf8 is better than latin9
\usepackage[T1]{fontenc}
%\usepackage{babel}% What language?
\usepackage[english]{babel}
\setcaptionfont{%
\color{red}
}
\usepackage{lipsum}

\begin{document}
\begin{margintable}
Red table
\caption{Red caption}
\end{margintable}

\lipsum[5]

\begin{figure}
Black text
\caption{Black Caption}
\end{figure}

\end{document}


# The problem with \setcaptionfont

As you mentioned in the comments below, the margintable contents are colored as well, which is undesired.

But I’m afraid this is by design (or perhaps a bug?).

The definition of \setcaptionfont is found in tufte-common.def:



which calls the internal command \@tufte@caption@font.

When \@tufte@caption@font is used in the “margin float environment”, on which marginfigure and margintable are based, it influences the whole float:

\newenvironment{@tufte@margin@float}[2][-1.2ex]%
{\FloatBarrier% process all floats before this point so the figure/table numbers stay in order.
\begin{lrbox}{\@tufte@margin@floatbox}%
\begin{minipage}{\marginparwidth}%
\@tufte@caption@font% <-- Your customized style is applied to the whole minipage
\def\@captype{#2}%
\hbox{}\vspace*{#1}%
\@tufte@caption@justification%
\@tufte@margin@par%
\noindent%
}
{\end{minipage}%
\end{lrbox}%
\marginpar{\usebox{\@tufte@margin@floatbox}}%
}


So, in the minds of the designers of tufte-book, you’re supposed to put only caption text in the margin note position. Well, except for graphics, and of course they are not affected by the font changing commands.

From a typographic point of view, I’d agree that you should never put tables in the margin note position (tables belong to the body text). If you insist, then there’s a workaround:

\documentclass[ngerman,nohyper]{tufte-book}
\usepackage[utf8]{inputenc}% utf8 is better than latin9
\usepackage[T1]{fontenc}
%\usepackage{babel}% What language?
\usepackage[english]{babel}
\setcaptionfont{%
\color{red}
}
\usepackage{lipsum}

% Define a font style switch for better coding.
% Add the following line to LyX: Document -> Settings -> LaTeX Preamble
\newcommand*\restoremarginfontstyle{\color{black}}

\begin{document}
\begin{margintable}
\begingroup% <-- Need to be inserted by Ctrl+L in LyX
\restoremarginfontstyle% <-- Switch used here, need to be inserted by Ctrl+L in LyX
Red table? Now black!
\endgroup% <-- Need to be inserted by Ctrl+L in LyX
\caption{Red caption}
\end{margintable}

\lipsum[5]

\begin{figure}
Black text
\caption{Black caption? Now red!}
\end{figure}

\end{document}


I don’t recommend doing this.

• Thanks for your answer, the package clash was indeed responsible for the second caption not being red -- but what about the "Red Table" still being red?? Even if I remove the caption completely, all margin content is formatted. :-( – Tim Eisenhardt Jul 14 '18 at 20:44
• @TimEisenhardt Did you mean “margin content is still colored” when you say “all margin content is formatted” – Ruixi Zhang Jul 14 '18 at 21:45
• @TimEisenhardt Please check my updated answer. – Ruixi Zhang Jul 14 '18 at 22:52
• Thanks for the great explanation. I use margin tables to convey additional examples such as densities of typical materials -- IMO this need not interrupt the explanation in the body text. And of course I don't want red captions, I only want them to be the same size as footnotes. Never thought it would be that hard... – Tim Eisenhardt Jul 15 '18 at 9:54
• @TimEisenhardt You’re welcome! An additional note: If you want the captions to be the same size as footnotes, then the default setting of \@tufte@marginfont suffices (which is \normalfont\footnotesize\sffamily or \normalfont\footnotesize), so you don’t need to modify anything. By doing \setcaptionfont{\color{red}} you are actually overwriting the command to use body text font size for the captions. – Ruixi Zhang Jul 15 '18 at 17:54