0

I am trying to reproduce, with minor differences, the caption style that I have seen in the microtype documentation:

1

I am using a the tufte-book class with the [openany] parameter so the margin is always on the right side of the page. An example table looks like this:

enter image description here

My approach is to use tcolorbox to frame my figure and tables (since I am using the package elsewhere and like its flexibility), draw an horizontal line from the box to margin and complete the line inside the caption by modifying its definition.

With my poor programming skills knowledge I hacked my way up to something that may be close to the result I want, although the code would most likely hurt the eyes of any decent LaTeX programmer. I modified the caption definition to remove the colon, introduce a line break, draw line as long as the caption width (but no longer that \linewidth) and fiddle with \parskip to adjust vertical spacing. Here is the code:

\newlength{\captlength}%will contain caption length
\makeatletter
%%% below is unchanged code from tufte definitions
\long\def\@caption#1[#2]#3{%
\par%
\addcontentsline{\csname ext@#1\endcsname}{#1}%
{\protect\numberline{\csname the#1\endcsname}{\ignorespaces #2}}%
\begingroup%
\@parboxrestore%
\if@minipage%
\@setminipage%
\fi%
\@tufte@caption@font\@tufte@caption@justification%
%%%% and below is the modified code
\settowidth{\captlength}{#3}%capture caption length
\ifdim \captlength > \linewidth%make sure \captlength is not greater than 
\linewidth
\setlength{\captlength}{\linewidth}
\fi %
\noindent\csname fnum@#1\endcsname\par\setlength{\parskip} 
{-1ex}\noindent\color{gray}\raisebox{0.65ex}{\rule{\captlength} 
{0.4pt}}\par\noindent\color{black}#3\par%add line breaks, gray rule and adjust 
vertical spacing
\endgroup}
\makeatother

Now making a table with tcolorbox as frame:

\begin{table}[ht]
\centering
\begin{tcolorbox}[enhanced, remember, hbox, tcbox width=auto limited, 
colback=white,arc=1pt,boxrule=0.4pt,colframe=gray]
\caption{A table with a caption.}
    \begin{tabular}{lll}
    first col. & second one & last one \\
    filling this & and this too & some more text
    \end{tabular}
\end{tcolorbox}
\end{table}

produces this:

Now I would need to draw the connecting horizontal line from the right edge of the box with a vertical offset so it can be at the same level with the caption rule using the tikz implementation of tcolorbox. I figured I would benefit from the tikzpagenodes package as well to get the margin node definition. The problem is that tikz is very foreign to me, as you can see from the lame attempt below:

\begin{table}[ht]
\centering
\begin{tcolorbox}[enhanced, remember, hbox, tcbox width=auto limited, 
colback=white,arc=1pt,boxrule=0.4pt,colframe=gray,
overlay={\draw[gray,line width=0.4pt](frame.north east) edge[->] (current page marginpar area.west);}]
\caption{A table with a caption.}
    \begin{tabular}{lll}
    first col. & second one & last one \\
    filling this & and this too & some more text
    \end{tabular}
\end{tcolorbox}
\end{table}

output:

I would appreciate any help. Thanks in advance! Here is the full code:

% !TEX program = xelatex
\documentclass[openany, justified]{tufte-book}

 \usepackage{tikzpagenodes,graphicx}
 \usepackage[most]{tcolorbox}

\newlength{\captlength}%will contain caption length
\makeatletter
\long\def\@caption#1[#2]#3{%
\par%
\addcontentsline{\csname ext@#1\endcsname}{#1}%
{\protect\numberline{\csname the#1\endcsname}{\ignorespaces #2}}%
\begingroup%
\@parboxrestore%
\if@minipage%
\@setminipage%
\fi%
\@tufte@caption@font\@tufte@caption@justification% 
\settowidth{\captlength}{#3}%capture caption length
\ifdim \captlength > \linewidth%make sure \captlength is not greater than 
\linewidth
\setlength{\captlength}{\linewidth}
\fi %
\noindent\csname fnum@#1\endcsname\par\setlength{\parskip} 
{-1ex}\noindent\color{gray}\raisebox{0.65ex}{\rule{\captlength} 
{0.4pt}}\par\noindent\color{black}#3\par%add line breaks, gray rule and adjust 
vertical spacing
\endgroup}
\makeatother

\begin{document}

\begin{table}[ht] 
\centering
\begin{tcolorbox}[enhanced, remember, hbox, tcbox width=auto limited, 
colback=white,arc=1pt,boxrule=0.4pt,colframe=gray,overlay={\draw[gray,line 
width=0.4pt](frame.north east) edge[->] (current page marginpar area.west);}]
\caption{A table with a caption.}
    \begin{tabular}{lll}
    first col. & second one & last one \\
    filling this & and this too & some more text
    \end{tabular}
 \end{tcolorbox}
 \end{table}

 \end{document}
2
  • If you want a horzontal line you can use (frame.north east) -- (frame.north -| current page marginpar area.west). Jan 17, 2022 at 1:04
  • @JohnKormylo Thanks! It is working... although I also need to offset vertically the line - I mean that the starting point must be below the top right corner of the box (i am guessing about one line height) so it can be at the same level with the caption rule. Do you know how to offset from the north east node?
    – Cabin
    Jan 17, 2022 at 1:23

1 Answer 1

0

Simplest is to use tikzlibrary calc notation. There is also [yshift=...] but it doesn't always work.

% !TEX program = xelatex
\documentclass[openany, justified]{tufte-book}

 \usepackage{tikzpagenodes,graphicx}
 \usepackage[most]{tcolorbox}
 \usetikzlibrary{calc}

\newlength{\captlength}%will contain caption length
\makeatletter
\long\def\@caption#1[#2]#3{%
\par%
\addcontentsline{\csname ext@#1\endcsname}{#1}%
{\protect\numberline{\csname the#1\endcsname}{\ignorespaces #2}}%
\begingroup%
\@parboxrestore%
\if@minipage%
\@setminipage%
\fi%
\@tufte@caption@font\@tufte@caption@justification% 
\settowidth{\captlength}{#3}%capture caption length
\ifdim \captlength > \linewidth%make sure \captlength is not greater than 
\linewidth
\setlength{\captlength}{\linewidth}
\fi %
\noindent\csname fnum@#1\endcsname\par\setlength{\parskip} 
{-1ex}\noindent\color{gray}\raisebox{0.65ex}{\rule{\captlength} 
{0.4pt}}\par\noindent\color{black}#3\par%add line breaks, gray rule and adjust 
vertical spacing
\endgroup}
\makeatother

\begin{document}

\begin{table}[ht] 
\centering
\begin{tcolorbox}[enhanced, remember, hbox, tcbox width=auto limited, 
colback=white,arc=1pt,boxrule=0.4pt,colframe=gray,overlay={\draw[gray,line 
width=0.4pt]($(frame.north east) + (0,-1ex)$) edge[->] ($(frame.north -| current page marginpar area.west) + (-1ex,-1ex)$);}]
\caption{A table with a caption.}
    \begin{tabular}{lll}
    first col. & second one & last one \\
    filling this & and this too & some more text
    \end{tabular}
 \end{tcolorbox}
 \end{table}

 \end{document}
1
  • Thank you very much for solving this John! I have adjusted the code so both lines align perfectly (and removed the arrow head): overlay={\draw[gray,line width=0.4pt]($(frame.north east) + (0,-2.2ex)$) -- ($(frame.north -| current page marginpar area.west) + (0ex,-2.2ex)$);}]
    – Cabin
    Jan 17, 2022 at 22:15

You must log in to answer this question.

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