我需要剪切页眉中出现的章节标题。
我正在开发一个乳胶生成器软件(http://github.com/caelum/tubaina,欢迎新的贡献者!),并且由于实现问题,我无法简单地使用\chaptermark
它来为标题创建任意标题。我尝试过使用\StrLeft
from xstring
,但对我来说不起作用。
下面是产生我想要看到的输出的乳胶代码,但是我无法使用:
\documentclass[a4paper, 11pt, twoside]{book}
\usepackage{fancyhdr}
\usepackage{xstring}
\usepackage{lipsum}
\begin{document}
\fancypagestyle{plain}{
\fancyhf{}
\fancyhead[LO,RE]{\footnotesize{Publisher Name}}
\fancyhead[LE]{\scriptsize{\nouppercase{\rightmark}}}
\fancyhead[RO]{\scriptsize{\nouppercase{\leftmark}}}
\fancyfoot[RO,LE]{\thepage}
}
\pagestyle{plain}
%% This doesn't work
% \renewcommand{\chaptermark}[1]{\markboth{\thechapter.\ \StrLeft{#1}{5}}{}}
\renewcommand{\chaptermark}[1]{\markboth{\thechapter.\ #1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection.\ #1}{}}
\chapter{Very long long long long long long long long long long long long long long chapter title}
%% This also doesn't work
% \chaptermark{\StrLeft{chapter mark lalala lalala}{5}}
%% This works and that's the way I would like to the
%% title appear, but I can't use it for other reasons...
\chaptermark{Very long long...}
\section{Section name}
\lipsum
\end{document}
还有其他方法可以做到这一点吗?
答案1
移动参数中的脆弱命令,因此您可以使用
\renewcommand{\chaptermark}[1]{\markboth{\thechapter.\ \protect\StrLeft{#1}{5}}{}}
尽管我认为不是在这么多字符后截断,而是通过将内容排版到临时框中的指定尺寸,然后只显示第一行,可以获得更好的效果。
类似这样的操作可以实现上面概述的方案:
\documentclass[a4paper, 11pt, twoside]{book}
\usepackage{fancyhdr}
\usepackage{xstring}
\usepackage{lipsum}
\begin{document}
\fancypagestyle{plain}{
\fancyhf{}
\fancyhead[LO,RE]{\footnotesize Publisher Name}
\fancyhead[LE]{\scriptsize\nouppercase{\rightmark}}
\fancyhead[RO]{\scriptsize\chopheader{\nouppercase{\leftmark}}}
\fancyfoot[RO,LE]{\thepage}
}
\pagestyle{plain}
\makeatletter
\def\chopheader#1{{%
\hbadness\@M
\vbadness\@M
\setbox\z@\vbox{%
\hsize=0.3\textwidth
\@parboxrestore\raggedright
\interlinepenalty-\@M
#1}%
\setbox\tw@\vsplit\z@ to \baselineskip
\setbox\tw@\vbox{\unvbox\tw@\global\setbox\@ne\lastbox}%
\leavevmode\unhbox\@ne\unskip\unskip\ifdim\ht\z@>\z@\ldots\fi
}}
\makeatother
%% This doesn't work
% \renewcommand{\chaptermark}[1]{\markboth{\thechapter.\ \protect\StrLeft{#1}{5}}{}}
%\renewcommand{\chaptermark}[1]{\markboth{\thechapter.\ #1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection.\ #1}{}}
\chapter{Very long long long long long long long long long long long long long long chapter title}
%% This also doesn't work
% \chaptermark{\StrLeft{chapter mark lalala lalala}{5}}
%% This works and that's the way I would like to the
%% title appear, but I can't use it for other reasons...
%\chaptermark{Very long long...}
\section{Section name}
\lipsum
\end{document}
答案2
某些宏的扩展xstring
并不是纯粹可扩展的,如文档- 部分3.2 宏的扩展,可选参数。这就是为什么作者为他们提供了一个可选参数来存储结果。您可以修改代码以使其工作,例如:
\renewcommand{\chaptermark}[1]{\StrLeft{#1}{5}[\shortTitle]%
\markboth{\thechapter.\ \shortTitle}{}}