tcolorbox:如何对齐标题的基线

tcolorbox:如何对齐标题的基线

我希望标题为personal和的两个框other显示在同一行,并与标题的基线对齐。到目前为止,我甚至无法让它们在同一行,尽管它们似乎有足够的空间。

% arara: pdflatex
% arara: pdflatex
% arara: open
\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\newtcolorbox{mybox}[2][]{
  enhanced,
  attach boxed title to top left={yshift=-2ex,xshift=4ex},
  colframe=black,
  colback=white,
  fonttitle=\bfseries, 
  colbacktitle=white,
  coltitle=black,
  boxed title style={
    boxrule=0pt,
    colframe=white,
    },
  title=#2,
  #1}

\setlength\parindent{0pt}
\pagestyle{empty}
\begin{document}

\begin{mybox}{hello world}
  this is a test
\end{mybox}


  \rule{0.45\columnwidth}{2pt}

  \begin{mybox}[width=0.45\columnwidth]{{personal}}
   These are the menu items:
   \begin{itemize}
   \item 
   \item 
   \item 
   \item 
   \item 
   \end{itemize}
  \end{mybox}%%
  \begin{mybox}[width=0.45\columnwidth]{{other}}
   These are the menu items:
   \begin{itemize}
   \item 
   \item 
   \item 
   \end{itemize}
  \end{mybox}%%

\end{document}

在此处输入图片描述

更新

现在,我已经设法通过使用按键将盒子放在同一条线上before=,after=

在此处输入图片描述

但它们仍未按照我的期望对齐。

tcolorbox我可以通过创建一个 strut 并将两个环境嵌入其中来实现我想要的效果tikzpicture

  \def\aes{\rule[-0.5ex]{0pt}{3ex}}

  \begin{tikzpicture}

    \node[outer sep=0pt,inner sep=0pt,anchor=north west]
         at (0,0) {%%
          \begin{mybox}[width=0.45\columnwidth]{{\aes personal}}
           These are the menu items:
           \begin{itemize}
           \item 
           \item 
           \item 
           \item 
           \item 
           \end{itemize}
          \end{mybox}%%
          };

    \node[outer sep=0pt,inner sep=0pt,anchor=north west]
       at (0.5\columnwidth,0) {%%
          \begin{mybox}[width=0.45\columnwidth]{{\aes other}}
           These are the menu items:
           \begin{itemize}
           \item 
           \item 
           \item 
           \end{itemize}
          \end{mybox}%%
       };

  \end{tikzpicture}

在此处输入图片描述

但似乎还有更多自然的方法来做到这一点。

答案1

一种方法是使用adjustbox包并应用:

before=\adjustbox{valign=t}\bgroup, after=\egroup

得出的结果是:

在此处输入图片描述

笔记:

  • 如果不想让两个框水平分开,请删除\hfill

  • 由于两个框中的标题深度不同,我\vphantom{p}在第二个框的标题中添加了。

参考:

代码:

\documentclass{article}
\usepackage{tcolorbox}
\usepackage{adjustbox}

\tcbuselibrary{skins}
\newtcolorbox{mybox}[2][]{
  enhanced,
  attach boxed title to top left={yshift=-2ex,xshift=4ex},
  colframe=black,
  colback=white,
  fonttitle=\bfseries, 
  colbacktitle=white,
  coltitle=black,
  boxed title style={
    boxrule=0pt,
    colframe=white,
    },
  title=#2,
  #1}

\setlength\parindent{0pt}
\pagestyle{empty}
\begin{document}

\begin{mybox}{hello world}
  this is a test
\end{mybox}


  \rule{0.45\columnwidth}{2pt}

  \begin{mybox}[width=0.45\columnwidth, before=\adjustbox{valign=t}\bgroup,
  after=\egroup\hfill]{{personal}}
   These are the menu items:
   \begin{itemize}
   \item 
   \item 
   \item 
   \item 
   \item 
   \end{itemize}
  \end{mybox}%%
  \begin{mybox}[width=0.45\columnwidth, before=\adjustbox{valign=t}\bgroup,
  after=\egroup]{{other\vphantom{p}}}
   These are the menu items:
   \begin{itemize}
   \item 
   \item 
   \item 
   \end{itemize}
  \end{mybox}%%

\end{document}

答案2

我喜欢这个adjustbox答案,因为它提供了一种将各种adjustbox功能结合在一起的优雅方法tcolorbox

更新答案:

如果只是为了顶部对齐:tcolorbox 3.30 (2014/11/17)上面有几个对齐和间距功能在这里很有用。第一个解决方案用于box align=top顶部对齐框:

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{skins}

\newtcolorbox{mybox}[2][]{
  enhanced,
  attach boxed title to top left={yshift=-2ex,xshift=4ex},
  colframe=black,
  colback=white,
  fonttitle=\bfseries,
  colbacktitle=white,
  coltitle=black,
  boxed title style={
    boxrule=0pt,
    colframe=white,
    },
  title=#2,
  #1}

\setlength\parindent{0pt}
\pagestyle{empty}
\begin{document}

\begin{mybox}{hello world}
  this is a test
\end{mybox}

\rule{0.45\columnwidth}{2pt}

\begin{mybox}[width=0.45\columnwidth,box align=top,before=\par,after=\hfill]{personal}
   These are the menu items:
   \begin{itemize}
   \item
   \item
   \item
   \item
   \item
   \end{itemize}
\end{mybox}%%
\begin{mybox}[width=0.45\columnwidth,box align=top,before=,after=\par]{other}
   These are the menu items:
   \begin{itemize}
   \item
   \item
   \item
   \end{itemize}
\end{mybox}%%

\end{document}

在此处输入图片描述

使用库可以实现相同的结果raster。这里,两个框被封装在一个tcbraster自动处理宽度和间距的环境中:

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{skins,raster}

\newtcolorbox{mybox}[2][]{
  enhanced,
  attach boxed title to top left={yshift=-2ex,xshift=4ex},
  colframe=black,
  colback=white,
  fonttitle=\bfseries,
  colbacktitle=white,
  coltitle=black,
  boxed title style={
    boxrule=0pt,
    colframe=white,
    },
  title=#2,
  #1}

\setlength\parindent{0pt}
\pagestyle{empty}
\begin{document}

\begin{mybox}{hello world}
  this is a test
\end{mybox}

\rule{0.45\columnwidth}{2pt}

\begin{tcbraster}[raster column skip=0.1\columnwidth,raster valign=top]
\begin{mybox}{personal}
   These are the menu items:
   \begin{itemize}
   \item
   \item
   \item
   \item
   \item
   \end{itemize}
\end{mybox}
\begin{mybox}{other}
   These are the menu items:
   \begin{itemize}
   \item
   \item
   \item
   \end{itemize}
\end{mybox}
\end{tcbraster}

\end{document}

另一种可能性是使用raster equal height在我看来对给定应用程序来说不错的功能。经过两次编译后,这两个框的高度相同:

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{skins,raster}

\newtcolorbox{mybox}[2][]{
  enhanced,
  attach boxed title to top left={yshift=-2ex,xshift=4ex},
  colframe=black,
  colback=white,
  fonttitle=\bfseries,
  colbacktitle=white,
  coltitle=black,
  boxed title style={
    boxrule=0pt,
    colframe=white,
    },
  title=#2,
  #1}

\setlength\parindent{0pt}
\pagestyle{empty}
\begin{document}

\begin{mybox}{hello world}
  this is a test
\end{mybox}

\rule{0.45\columnwidth}{2pt}

\begin{tcbraster}[raster column skip=0.1\columnwidth,raster equal height]
\begin{mybox}{personal}
   These are the menu items:
   \begin{itemize}
   \item
   \item
   \item
   \item
   \item
   \end{itemize}
\end{mybox}
\begin{mybox}{other}
   These are the menu items:
   \begin{itemize}
   \item
   \item
   \item
   \end{itemize}
\end{mybox}
\end{tcbraster}

\end{document}

在此处输入图片描述

答案3

嗯,你总是可以把它们放在顶部对齐minipages

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\newtcolorbox{mybox}[2][]{
  enhanced,
%  attach boxed title to top left={yshift=-2ex,xshift=4ex},
  colframe=black,
  colback=white,
  fonttitle=\bfseries, 
  colbacktitle=white,
  coltitle=black,
%  boxed title style={
%    boxrule=0pt,
%    colframe=white,
%    },
  title=#2,
  #1}

\setlength\parindent{0pt}
\pagestyle{empty}
\begin{document}

\begin{mybox}{hello world}
  this is a test
\end{mybox}


  \rule{0.45\columnwidth}{2pt}

\begin{minipage}[t]{.5\textwidth}
\centering
  \begin{mybox}[width=0.9\columnwidth]{{personal}}
   These are the menu items:
   \begin{itemize}
   \item 
   \item 
   \item 
   \item 
   \item 
   \end{itemize}
  \end{mybox}%%
\end{minipage}
\begin{minipage}[t]{.5\textwidth}
\centering
  \begin{mybox}[width=0.9\columnwidth]{{other}}
   These are the menu items:
   \begin{itemize}
   \item 
   \item 
   \item 
   \end{itemize}
  \end{mybox}%%
\end{minipage}

\end{document}

在此处输入图片描述

答案4

也许这不太合适,但最终我放弃了使用tcolorbox(因为我的问题是关于使用tcolorbox)。我一直遇到太多麻烦,无法让事情按照我想要的方式运行。但是,我将来会继续探索这个包,并更好地了解它的可能性。

不过,我最终做的只是从地面开始构建我的盒子,tikz我更熟悉的地方。(直到我最终将我的两个tcolorboxs 嵌入到 中时,我才想到使用这种方法tikzpicture。)

此外,我注意到我想要对齐的不是标题的基线,而是围绕盒装内容的框架的顶部。

这可以稍微清理一下。但总的来说,我想做的事情相当简单:

% arara: pdflatex
% arara: pdflatex
% arara: open
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\makeatletter

\def\ae@mybox@width{3in}
\def\ae@mybox@title{}
\pgfkeys{/ae/mybox/.cd,
  width/.store in=\ae@mybox@width,
  title/.store in=\ae@mybox@title,
}

\newsavebox\ae@mybox

\newenvironment{mybox}[1][]
  {\pgfkeys{/ae/mybox/.cd,#1}%%
   \begin{lrbox}{\ae@mybox}%
    \begin{minipage}{\ae@mybox@width}
  }
  {
    \end{minipage}%%
    \end{lrbox}%%
    \begin{tikzpicture}[baseline=(title.west)]
      \node[outer sep=0pt,
            anchor=north west,
            inner sep=8pt] (MP)    at (0,0)                         {\usebox{\ae@mybox}};
      \node[anchor=west]   (title) at ($(MP.north west)+(1em,2ex)$) {\ae@mybox@title};
      \draw [rounded corners]
          (title.west) -- (MP.west |- title.west) 
                       -- (MP.south west)
                       -- (MP.south east)
                       -- (MP.north east|-title.east)
                       -- (title.east);
    \end{tikzpicture}}

\makeatother

\setlength\parindent{0pt}
\begin{document}

\begin{mybox}[width=\dimexpr0.45\columnwidth-16pt,title=personal]
 These are the menu items:
 \begin{itemize}
 \item 
 \item 
 \item 
 \item 
 \item 
 \end{itemize}
\end{mybox}%%
\hspace{\fill}%%
\begin{mybox}[width=0.45\columnwidth-16pt,title=other]
 These are the menu items:
 \begin{itemize}
 \item 
 \item 
 \end{itemize}
\end{mybox}%%

\end{document}

在此处输入图片描述

相关内容