exam/tcolorbx:计算标题高度然后将其传递给几何

exam/tcolorbx:计算标题高度然后将其传递给几何

对于以下内容,我需要使这两种方法发挥作用,以便将精确的标题高度传递给geometry包。

如果有其他方法可以提出,我会很高兴。

使用 savebox

\documentclass{exam}
\usepackage{graphicx}
\usepackage[xparse,breakable,raster]{tcolorbox}

\newsavebox{\headerbox}
\newcommand{\TCheader}{%
    \savebox{\headerbox}% 
    {%
        \begin{tcbraster}[
            raster columns=3, raster rows=1, valign=center,
            raster height = 2cm, raster column skip = 0mm
            ]
            \begin{tcolorbox}[left=0pt,right=0pt]
                First line\\ Second Line
            \end{tcolorbox}
            \begin{tcolorbox}[halign=center]
                Centered Text
            \end{tcolorbox}
            \begin{tcolorbox}[halign=right]
                \includegraphics[height=2cm]{example-image-a}
            \end{tcolorbox}
        \end{tcbraster}%
    }%
}

\pagestyle{headandfoot}

\firstpageheader{}{\TCheader}{}

\usepackage[
includehead, 
top = 10 mm,
headheight =  \the\ht\headerbox
]{geometry}

\begin{document}
text
\usebox\headerbox
\end{document}

使用 settoheight

\documentclass{exam}
\usepackage{graphicx}
\usepackage[xparse,breakable,raster]{tcolorbox}

\newcommand{\Anotherheader}{%
    \settoheight{\headerheight}% 
    {%
        \begin{tcbraster}[
            raster columns=3, raster rows=1, valign=center,
            raster height = 2cm, raster column skip = 0mm
            ]
            \begin{tcolorbox}[left=0pt,right=0pt]
                First line\\ Second Line
            \end{tcolorbox}
            \begin{tcolorbox}[halign=center]
                Centered Text
            \end{tcolorbox}
            \begin{tcolorbox}[halign=right]
                \includegraphics[height=2cm]{example-image-b}
            \end{tcolorbox}
        \end{tcbraster}%
    }%
}

\pagestyle{headandfoot}

\firstpageheader{}{\Anotherheader}{}

\usepackage[
includehead, 
top = 10 mm,
headheight =  \headerheight
]{geometry}

\begin{document}
text
\end{document}

答案1

Atcbraster需要一些信封作为minipage放入盒子中。

对于您的应用程序,我将使用可供以后保存的tcboxedraster内部光栅。tcolorbox

恕我直言,无需计算身高,因为身高已经知道了(2cm)。

\documentclass{exam}
\usepackage{graphicx}
\usepackage[xparse,breakable,raster,skins]{tcolorbox}

\def\headerContents{%
  \begin{tcboxedraster}[
      raster columns=3, raster rows=1, valign=center,
      raster height = 2cm, raster column skip = 0mm
      ]{blankest, nobeforeafter, baseline=\baselineskip, enlarge bottom finally by=-0.4pt}
      \begin{tcolorbox}[left=0pt,right=0pt]
          First line\\ Second Line
      \end{tcolorbox}
      \begin{tcolorbox}[halign=center]
          Centered Text
      \end{tcolorbox}
      \begin{tcolorbox}[halign=right]
          \includegraphics[height=2cm]{example-image-a}
      \end{tcolorbox}
  \end{tcboxedraster}%
}

\pagestyle{headandfoot}
\firstpageheader{}{\headerContents}{}

\usepackage[
  includehead,
  top = 0mm, % 0mm is just for test
  %headheight = \headerTotalHeight,
  headheight = 2cm,
  showframe % draw frames to show page layout
]{geometry}

\begin{document}
text
\end{document}

对上面代码的一些解释:

  • baseline=\baselineskip用于降低tikzpicture基线以下。\baselineskip绰绰有余(可以用更好的东西代替),但在这里完成了工作。
  • enlarge bottom finally by=-0.4pt只是微调覆盖了粗细为 的标题行0.4pt。这样就可以完美贴合页面上边缘。

答案2

在序言中直接将tcbraster环境保存到框中会引发Missing \begin{document}错误,这是由\noindent末尾的引起的\begin{tcbraster}。因此我将其包装tcbrasterminipage环境中。还使用了\settototalheightfrom包。calc

这可行,但恐怕这不是完美的解决方案。

\documentclass{exam}
\usepackage{graphicx}
\usepackage[xparse,breakable,raster]{tcolorbox}
\usepackage{calc}

\newcommand\headerContents{%
  \begin{minipage}{\linewidth}
    \begin{tcbraster}[
        raster columns=3, raster rows=1, valign=center,
        raster height = 2cm, raster column skip = 0mm
        ]
        \begin{tcolorbox}[left=0pt,right=0pt]
            First line\\ Second Line
        \end{tcolorbox}
        \begin{tcolorbox}[halign=center]
            Centered Text
        \end{tcolorbox}
        \begin{tcolorbox}[halign=right]
            \includegraphics[height=2cm]{example-image-a}
        \end{tcolorbox}
    \end{tcbraster}%
  \end{minipage}%
}

\newlength{\headerTotalHeight}
\settototalheight\headerTotalHeight{\headerContents}

\pagestyle{headandfoot}
\firstpageheader{}{\headerContents}{}

\usepackage[
  includehead, 
  top = 0mm, % 0mm is just for test
  headheight = \headerTotalHeight,
  showframe % draw frames to show page layout
]{geometry}

\begin{document}
text
\end{document}

在此处输入图片描述

答案3

请注意,加载几何形状会发生变化\textwidth,因此我立即加载它,然后用它\geometry来设置最终尺寸。

\documentclass{exam}
\usepackage[showframe]{geometry}% affects \textwidth
\usepackage{graphicx}
\usepackage[xparse,breakable,raster]{tcolorbox}

\newsavebox\headerbox

\savebox{\headerbox}{%
  \begin{minipage}{\textwidth}
    \begin{tcbraster}[
        raster columns=3, raster rows=1, valign=center,
        raster height = 2cm, raster column skip = 0mm
        ]
        \begin{tcolorbox}[left=0pt,right=0pt]
            First line\\ Second Line
        \end{tcolorbox}
        \begin{tcolorbox}[halign=center]
            Centered Text
        \end{tcolorbox}
        \begin{tcolorbox}[halign=right]
            \includegraphics[height=2cm]{example-image-a}
        \end{tcolorbox}
    \end{tcbraster}%
  \end{minipage}%
}
\geometry{includehead, top = 10 mm, headheight={\dimexpr \ht\headerbox+\dp\headerbox}}

\pagestyle{headandfoot}
\firstpageheader{}{\usebox\headerbox}{}

\begin{document}
text

\noindent\usebox\headerbox
\end{document}

相关内容