有没有办法让小 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
两个选项;第一个选项使用baseline
es 键进行手动调整tcolorbox
,第二个选项使用包中的valign=c
选项;使用和自动完成设置:\adjustbox
adjustbox
before
after
\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
,但也可以选择top
、center
和base
。因此,对 进行一点修改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}