tcolorbox 的“nobeforeafter”垂直对齐

tcolorbox 的“nobeforeafter”垂直对齐

有没有办法让小 tcolorboxes 垂直居中?

\documentclass[]{article}
\usepackage[skins]{tcolorbox}

\begin{document}

\newtcbox{\LittleBox}[1][]{enhanced,nobeforeafter,#1}%

\begin{tcolorbox}[enhanced,sidebyside,sidebyside align= center, colbacktitle=white,coltitle=black,colback=white,fonttitle=\large\bfseries,title= Little boxes not centered vertically]

% not working: valign=center,valign lower=center, before=  \begin{center}, \vfill, \[\vcenter{}\], \begin{tabular}{cc}\end{tabular},sidebyside align= center,   \centering

\LittleBox{\scriptsize First} 
\LittleBox{\huge Second}
\tcblower
\LittleBox{\Large Third}
\LittleBox{\small Fourth}

\end{tcolorbox}

在此处输入图片描述

答案1

两个选项;第一个选项使用baselinees 键进行手动调整tcolorbox,第二个选项使用包中的valign=c选项;使用和自动完成设置:\adjustboxadjustboxbeforeafter

\newtcbox{\LittleBox}[1][]{
  enhanced,
  before=\adjustbox{valign=c}\bgroup,
  after=\egroup,#1
}

包含两种可能性的完整代码:

\documentclass[]{article}
\usepackage[skins]{tcolorbox}
\usepackage{adjustbox}

\newtcolorbox{mybox}[1][]{
  enhanced,
  sidebyside,
  sidebyside align= center,   
  colbacktitle=white,
  coltitle=black,
  colback=white,
  fonttitle=\large\bfseries,
  #1
}
\newtcbox{\LittleBox}[1][]{
  enhanced,
  before=\adjustbox{valign=c}\bgroup,
  after=\egroup,#1
}
\newtcbox{\LittleBoxA}[1][]{
  enhanced,
  nobeforeafter,#1
}

\begin{document}

\begin{mybox}[title= Little boxes centered vertically]
\LittleBoxA[baseline=-1ex]{\scriptsize First} 
\LittleBoxA{\huge Second}
\tcblower
\LittleBoxA{\Large Third}
\LittleBoxA[baseline=-0.5ex]{\small Fourth}
\end{mybox}

\begin{mybox}[title= Little boxes centered vertically]
\LittleBox{\scriptsize First} 
\LittleBox{\huge Second}
\tcblower
\LittleBox{\Large Third}
\LittleBox{\small Fourth}
\end{mybox}

\end{document}

在此处输入图片描述

答案2

tcolorbox在2014-10-10发布的版本中引入了box align解决该问题的选项。

box align的默认值为bottom,但也可以选择topcenterbase。因此,对 进行一点修改LittleBox即可

\newtcbox{\LittleBox}[1][]{enhanced, nobeforeafter, box align = center, #1}

在此处输入图片描述

\documentclass[]{article}
\usepackage[skins]{tcolorbox}

\begin{document}

\newtcbox{\LittleBox}[1][]{enhanced, nobeforeafter, box align = center, #1}%

\begin{tcolorbox}[enhanced, sidebyside, sidebyside align= center, 
    colbacktitle=white, coltitle=black, colback=white, 
    fonttitle=\large\bfseries, title= Little boxes centered vertically]

\LittleBox{\scriptsize First} 
\LittleBox{\huge Second}
\tcblower
\LittleBox{\Large Third}
\LittleBox{\small Fourth}

\end{tcolorbox}

\end{document}

相关内容