考虑以下代码:
\documentclass{article}
\usepackage{calc}
\usepackage{lipsum}
\usepackage{xcolor}
\usepackage{scrlayer}
\newcommand{\headerheight}{2cm}
\newcommand{\headertext}{\Huge\textcolor{red}{Header}}
\DeclareNewLayer[
background,
area={20pt}{30pt}{\paperwidth}{\headerheight},
contents={%
\parbox[c][\layerheight]{\layerwidth}{}
}
]{layer1}
\DeclareNewLayer[
clone=layer1,
contents={\headertext}
]{layer2}
\DeclarePageStyleByLayers{teststyle}{layer2, layer1}
\pagestyle{teststyle}
\begin{document}
\lipsum
\end{document}
Header
它生成一个文档,每个页面的左上角都写有单词。相反,我想让我的页面样式可自定义,这样我就可以编写类似下面的代码(语法可能不同,但它给出了一个粗略的概念):
\pagestyle[headertitle = This is my title, leftshift = 40pt]{teststyle}
使用如下定义的命令:
\newcommand{\headertext}[1]{\Huge\textcolor{red}{#1}}
\DeclareNewLayer[
background,
area={<leftshift (defined as 20pt by default)>}{<rightshift (defined as 30pt by default)>}{\paperwidth}{\headerheight},
contents={%
\parbox[c][\layerheight]{\layerwidth}{}
}
]{layer1}
\DeclareNewLayer[
clone=layer1,
contents={\headertext{<headertitle>}}
]{layer2}
其中“变量” leftshit
、rightshift
和headertitle
已经放入<>
(仅用于说明)。
如何实现这一目标?
答案1
我想知道你为什么要定义这样的宏。但也许以下建议有助于实现你的目标:
\documentclass{article}
\usepackage{lipsum}
\usepackage{xcolor}
\usepackage{scrlayer}
\newcommand*{\headerheight}{2cm}
\newcommand*{\headertext}{}
\newkomafont{headertext}{\Huge\color{red}}
\DeclareNewLayer[
background,
area={20pt}{30pt}{\paperwidth}{\headerheight},
contents={%
\parbox[c][\layerheight]{\layerwidth}{}% Why there is an empty parbox?
}
]{layer1}
\DeclareNewLayer[
clone=layer1,
]{layer2}
\DeclarePageStyleByLayers{teststyle}{layer2, layer1}
\newcommand*\setpagestyle[2][]{%
\RedeclareLayer[clone=layer1]{layer2}% reset layer2 as clone of layer1
\ModifyLayer[#1,pretocontents=\usekomafont{headertext}]{layer2}% add the optional settings
\pagestyle{#2}% set the pagestyle
}
\begin{document}
\setpagestyle[contents=Header]{teststyle}
\lipsum
\clearpage
\setpagestyle[hoffset=60pt,voffset=1cm,contents=This is my title]{teststyle}
\lipsum
\clearpage
\setpagestyle[contents=Header]{teststyle}
\lipsum
\end{document}
结果: