lrbox/minipage/colorbox 内容中垂直间距宏的奇怪解释

lrbox/minipage/colorbox 内容中垂直间距宏的奇怪解释

我知道我以前见过这个排版问题,但我找不到解决方案。latex 可能相当违反直觉。我所做的就是创建一个 \panel{} 标题类型宏,该宏在稍微修改后的表格内工作。\panel 应该增加与前一个表格的间距并将其缩小到下一个表格。

我已经简化了一个例子来展示这个问题。如何定义一个 \panel 宏来执行预期的操作?

\documentclass{article}

\usepackage{color}
\usepackage{booktabs}

\newsavebox{\selvesteboxtwo}
\newenvironment{colboxfigtbl}
  {
   \begin{lrbox}{\selvesteboxtwo}%
   \begin{minipage}{\dimexpr\columnwidth-2\fboxsep-4em\relax}}
  {
   \end{minipage}\end{lrbox}%
   \begin{center}%
     \fcolorbox{blue}{yellow}{\usebox{\selvesteboxtwo}}%%real life do more
   \end{center} 
  }

\newenvironment{newtable}[1][]{%
  \begin{table}%
    \begin{center}%
      \begin{colboxfigtbl}
      }{%
      \end{colboxfigtbl}%
    \end{center}%
  \end{table}
}

\newcommand{\panel}[2]{%
  \par\bigskip%
  \begin{center} \textbf{Panel #1:} #2 \end{center}%
  \par\vspace*{-1.5\baselineskip}%
}

\begin{document}

\begin{newtable}

  \panel{A}{This is a bad panel}

  \begin{center}\begin{tabular}{l r} \toprule A & B \\ \bottomrule \end{tabular} \end{center}

  \panel{B}{This is a better panel}

  \begin{center}\begin{tabular}{l r} \toprule A & B \\  \bottomrule \end{tabular} \end{center}

  \panel{C}{This is a better panel}

  \begin{center}\begin{tabular}{l r} \toprule A & B \\  \bottomrule \end{tabular} \end{center}

\end{newtable}

\end{document}

答案1

通常,将环境嵌套在一起可能很棘手,因为环境往往会在其内容的上方和下方添加空间。除此之外,一些环境会变得聪明,减去嵌套环境插入的额外空间。对于您这样的情况,答案很简单:在\newtable已经调用table环境的环境中,将嵌套\begin{center}...\end{center}环境替换为\centering...,这是一种无需环境即可实现相同效果的宏形式。

\documentclass{article}

\usepackage{color}
\usepackage{booktabs}

\newsavebox{\selvesteboxtwo}
\newenvironment{colboxfigtbl}
  {
   \begin{lrbox}{\selvesteboxtwo}%
   \begin{minipage}{\dimexpr\columnwidth-2\fboxsep-4em\relax}}
  {
   \end{minipage}\end{lrbox}%
   \begin{center}%
     \fcolorbox{blue}{yellow}{\usebox{\selvesteboxtwo}}%%real life do more
   \end{center} 
  }

\newenvironment{newtable}[1][]{%
  \begin{table}%
    \centering%\begin{center}%
      \begin{colboxfigtbl}
      }{%
      \end{colboxfigtbl}%
%    \end{center}%
  \end{table}
}

\newcommand{\panel}[2]{%
  \par\bigskip%
  \begin{center} \textbf{Panel #1:} #2 \end{center}%
  \par\vspace*{-1.5\baselineskip}%
}

\begin{document}

\begin{newtable}

  \panel{A}{This is a bad panel}

  \begin{center}\begin{tabular}{l r} \toprule A & B \\ \bottomrule \end{tabular} \end{center}

  \panel{B}{This is a better panel}

  \begin{center}\begin{tabular}{l r} \toprule A & B \\  \bottomrule \end{tabular} \end{center}

  \panel{C}{This is a better panel}

  \begin{center}\begin{tabular}{l r} \toprule A & B \\  \bottomrule \end{tabular} \end{center}

\end{newtable}

\end{document}

在此处输入图片描述

相关内容