使用 tufte-book 类中的 tcolorbox 和 tikz 绘制与图形/表格框架连接的标题规则

使用 tufte-book 类中的 tcolorbox 和 tikz 绘制与图形/表格框架连接的标题规则

我试图重现我在微类型文档中看到的标题样式(略有不同):

1

我使用带有参数的 tufte-book 类,[openany]因此边距始终位于页面的右侧。示例表如下所示:

在此处输入图片描述

我的方法是用tcolorbox它来构建我的图形和表格(因为我在其他地方使用该包并且喜欢它的灵活性),从框到边缘画一条水平线,并通过修改其定义来完成标题内的线条。

由于我编程技能有限,我只能自己动手,想出一个接近我想要的结果,尽管代码可能会伤害任何优秀的 LaTeX 程序员的眼睛。我修改了标题定义,删除了冒号,引入了换行符,绘制了与标题宽度一样长的线(但不再是那么长\linewidth),并\parskip调整了垂直间距。代码如下:

\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

现在制作一个带有如下框架的表格tcolorbox

\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}

产生这个:

现在我需要从框的右边缘绘制连接水平线,并进行垂直偏移,以便使用 tcolorbox 的 tikz 实现与标题规则处于同一水平。我想我tikzpagenodes也可以从该包中受益,以获得边距节点定义。问题是 tikz 对我来说非常陌生,正如您从下面的蹩脚尝试中看到的那样:

\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}

输出:

我将不胜感激任何帮助。提前致谢!以下是完整代码:

% !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}

答案1

最简单的方法是使用 tikzlibrary calc 符号。也有,[yshift=...]但并不总是有效。

% !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}

相关内容