以下是几个问题:
1-如何使第一光栅行的使用\tcboxfit
不扭曲布局,如下所示?
headheight
2-如何精确计算传递给选项的尺寸总和geometry
,以便页眉恰好从页面顶部边框开始?
3- 为什么不在\iflastpage{\enlargethispage{-1cm}}{}
里面\NewDocumentCommand{\FillAnswerRules}
为文本腾出空间End of the Exam
?
\baselineskip
4-为什么尽管设置了,但页眉和主流文本之间的空格(接近)并没有消除headsep = 0pt
?
\documentclass{exam}
\usepackage[fontsize = 14pt]{scrextend}
\usepackage{lipsum}
\usepackage[xparse,breakable,raster,skins]{tcolorbox}
\tcbuselibrary{fitting}
\pagestyle{headandfoot}
\newcommand{\myfooter}{%
\iflastpage{\raisebox{5pt}[0pt][0pt]{\large End of the Exam}}{}%
\hrule\vspace{5pt}%
Instructors%
}
\newcommand\headerBody{%
\begin{tcbitemize}[
raster valign = bottom,
valign = bottom,
raster columns = 2,
blankest,
raster after skip = 1ex,
]
\tcboxfit[height = 3em, add to width = 2cm, halign = flush left,height=3em] University\\Faculty\\Program
\tcbitem[add to width = -2cm, halign = flush right,] \includegraphics[height = 3em]{example-image}
\end{tcbitemize}
%
\hrule
%
\begin{tcbraster}[
raster columns = 3,
valign = center,
raster before skip = 1ex,
raster after skip = 1ex,
blankest
]
\tcboxfit{Final\\Semester}
\tcboxfit[before upper*=\centering]{Course Code}
\tcboxfit[halign = flush right]{\textbf{date}\\\textbf{time}}
\end{tcbraster}
%
\hrule
%
\begin{tcbraster}[
raster columns = 1,
valign = center, halign = flush center,
raster before skip = 1ex,
raster after skip = 1ex,
blankest
]
\tcboxfit[interior engine = standard,colback = gray!20, top = 1ex, bottom = 1ex,]{\textbf{Course}}%
\end{tcbraster}
%
\hrule
%
\begin{tcbraster}[
raster columns = 1,
valign = center, halign = flush center,
raster before skip = 1ex,
raster after skip = 1ex,
blankest,
]
\tcboxfit[top = 1ex, bottom = 1ex,]{This exam contains.}%
\end{tcbraster}
%
\hrule%
}
\newcommand\headerContents{% https://tex.stackexchange.com/a/547910/2288
\begin{tcbraster}[
fit algorithm=hybrid*,
raster equal height = rows,
raster halign = center, raster valign = center,
raster equal skip = 0mm,
raster force size = false,
boxsep = 0pt,
beforeafter skip = 0pt,
leftright skip = 0pt,
raster right skip = 0pt,
raster left skip = 0pt,
raster before skip = 0pt,
raster after skip = 0pt,
nobeforeafter,
height = 2em,
raster rows=4,
]%
\headerBody%
\end{tcbraster}%
}
\firstpageheader{}{\headerContents}{}
\runningheader{}{\headerContents}{}
\firstpagefooter{}{\myfooter}{}
\runningfooter{}{\myfooter}{}
\ExplSyntaxOn
\keys_define:nn { diaa / answers }
{
d .dim_set:N = \l__diaa_answers_distance_dim,
t .dim_set:N = \l__diaa_answers_thickness_dim,
c .tl_set:N = \l__diaa_answers_color_tl,
d .initial:n = 10mm,
t .initial:n = 0.2pt,
c .initial:n = gray,
}
\NewTColorBox { fillAnswerRules } { O{} }
{
height~fill, text~fill, colback=white, boxrule=2pt, sharp~corners,
boxsep=0pt, left=10pt, right=10pt, top=8pt, bottom=0pt, #1
}
\NewDocumentCommand{\FillAnswerRules}{O{}}
{
\iflastpage{\enlargethispage{-1cm}}{}
\begin{fillAnswerRules}
\keys_set:nn { diaa / answers } { #1 }
\leaders \hbox:n
{
\makebox[\textwidth][s]
{
\color{\l__diaa_answers_color_tl}
\vrule width 0pt height 0.5\l__diaa_answers_distance_dim
depth 0.5\l__diaa_answers_distance_dim
\leaders \hrule height \l__diaa_answers_thickness_dim \hfill
}
}\vfill
\null
\end{fillAnswerRules}
}
\ExplSyntaxOff
\usepackage[
a4paper,
includehead,
headsep=0pt,
includefoot,
foot=0mm,
bottom=20mm,
headheight = \dimexpr9em+9ex+0pt,
top=0mm,
showframe
]{geometry}
\begin{document}
\begin{questions}
\question
\begin{parts}
\part \lipsum[1-2]
\FillAnswerRules
\FillAnswerRules
\part \lipsum[1]
\FillAnswerRules
\end{parts}
\end{questions}
\end{document}
答案1
此解决方案将页眉放入保存框中,以便准确计算页眉高度。它还会在\iflastpage
内部之前增加页计数器\FillAnswerRules
。
请注意,\tcb@startbox
等直到才定义\AtBeginDocument
。
\documentclass{exam}
\usepackage[fontsize = 14pt]{scrextend}
\usepackage{lipsum}
\usepackage[xparse,breakable,raster,skins]{tcolorbox}
\tcbuselibrary{fitting}
\pagestyle{headandfoot}
\newcommand{\myfooter}{%
\iflastpage{\raisebox{\dimexpr \height+\depth+5pt}[0pt][0pt]{\large End of the Exam}}{}%
\hrule\vspace{5pt}%
Instructors%
}
\newcommand\headerBody{%
\begin{tcbitemize}[
raster valign = bottom,
valign = bottom,
raster columns = 2,
blankest,
raster after skip = 1ex,
]
\tcboxfit[height = 3em, add to width = 2cm, halign = flush left,height=3em] {University\\Faculty\\Program}
\tcbitem[add to width = -2cm, halign = flush right,] \includegraphics[height = 3em]{example-image}
\end{tcbitemize}
%
\hrule
%
\begin{tcbraster}[
raster columns = 3,
valign = center,
raster before skip = 1ex,
raster after skip = 1ex,
blankest
]
\tcboxfit{Final\\Semester}
\tcboxfit[before upper*=\centering]{Course Code}
\tcboxfit[halign = flush right]{\textbf{date}\\\textbf{time}}
\end{tcbraster}
%
\hrule
%
\begin{tcbraster}[
raster columns = 1,
valign = center, halign = flush center,
raster before skip = 1ex,
raster after skip = 1ex,
blankest
]
\tcboxfit[interior engine = standard,colback = gray!20, top = 1ex, bottom = 1ex,]{\textbf{Course}}%
\end{tcbraster}
%
\hrule
%
\begin{tcbraster}[
raster columns = 1,
valign = center, halign = flush center,
raster before skip = 1ex,
raster after skip = 1ex,
blankest,
]
\tcboxfit[top = 1ex, bottom = 1ex,]{This exam contains.}%
\end{tcbraster}
%
\hrule%
}
\usepackage[a4paper,
includehead,
headheight=0pt,% will be reset \AtBeginDocument
headsep=0pt,
includefoot,
foot=0mm,
bottom=20mm,
top=0mm,
showframe]{geometry}
\newsavebox{\headerbox}
\AtBeginDocument{\savebox{\headerbox}{\begin{minipage}{\textwidth}
\begin{tcbraster}[
fit algorithm=hybrid*,
raster equal height = rows,
raster halign = center, raster valign = center,
raster equal skip = 0mm,
raster force size = false,
boxsep = 0pt,
beforeafter skip = 0pt,
leftright skip = 0pt,
raster right skip = 0pt,
raster left skip = 0pt,
raster before skip = 0pt,
raster after skip = 0pt,
nobeforeafter,
height = 2em,
raster rows=4,
]%
\headerBody%
\end{tcbraster}%
\end{minipage}}
\geometry{headheight = {\dimexpr \ht\headerbox+\dp\headerbox}}
\enlargethispage{-\headheight}% first page only
}% end of \AtBeginDocument
\firstpageheader{}{\usebox\headerbox}{}
\runningheader{}{\usebox\headerbox}{}
\AddToHookNext{shipout/background}{\put({\dimexpr 1in+\oddsidemargin},{\dimexpr 20mm-\paperheight-\baselineskip})%
{\parbox[t]{\textwidth}{\centering\myfooter}}}% first page footer
%\firstpagefooter{}{\myfooter}{}
\runningfooter{}{\myfooter}{}
\ExplSyntaxOn
\keys_define:nn { diaa / answers }
{
d .dim_set:N = \l__diaa_answers_distance_dim,
t .dim_set:N = \l__diaa_answers_thickness_dim,
c .tl_set:N = \l__diaa_answers_color_tl,
d .initial:n = 10mm,
t .initial:n = 0.2pt,
c .initial:n = gray,
}
\NewTColorBox { fillAnswerRules } { O{} }
{
height~fill, text~fill, colback=white, boxrule=2pt, sharp~corners,
boxsep=0pt, left=10pt, right=10pt, top=8pt, bottom=0pt, #1
}
\NewDocumentCommand{\FillAnswerRules}{O{}}
{\stepcounter{page}\iflastpage{\enlargethispage{-1cm}}{}\addtocounter{page}{-1}% does not affect footer location
\begin{fillAnswerRules}
\keys_set:nn { diaa / answers } { #1 }
\leaders \hbox:n
{
\makebox[\textwidth][s]
{
\color{\l__diaa_answers_color_tl}
\vrule width 0pt height 0.5\l__diaa_answers_distance_dim
depth 0.5\l__diaa_answers_distance_dim
\leaders \hrule height \l__diaa_answers_thickness_dim \hfill
}
}\vfill
\null
\end{fillAnswerRules}
}
\ExplSyntaxOff
\begin{document}
\begin{questions}
\question
\begin{parts}
\part \lipsum[1-2]
\FillAnswerRules
\FillAnswerRules
\part \lipsum[1]
\FillAnswerRules
\end{parts}
\end{questions}
\end{document}