是否可以将 TeX 宏与某些文本(例如下一段)或另一个宏绑定?
供您参考,以下是该问题的更具体描述:
我正在使用设置宏
\responsible{myname}
这只是将“myname”放入页脚,直到再次使用该命令与另一个选项。因此它只是扩展为类似以下内容:
\def\responsible#1{\ifoot{#1}}
我遇到的问题是,LaTeX 有时会在段落(或\chapter
、\section
、...)前插入分页符。由于作者姓名经常在各个页面之间变化,因此\responsible
必须在同一页面上展开相应的文本。
一方面,很明显,当涉及章节或节命令时,
\responsible
可以在章节命令后使用以确保正确功能。另一方面,此解决方案不适用于文本段落。
%% This works fine
\chapter{My Chapter}
\responsible{myname}
%% This:
\responsible{myname}
example text, example text, example text.
%% or this does not work
\responsible{myname}
\chapter{My Chapter}
是否可以将命令绑定到以下段落而不影响字符或单词间距? 如果插入的空格以某种方式被 TeX 忽略,那么 ~ 甚至可能是一个可行的解决方案。
例如解决方案可能如下所示:
\responsible{myname}\tieto example text, example text.\par
甚至:
\def\responsible#1{\ifoot{#1}\tieto}
编辑:针对章节/部分问题的 MWE:
\documentclass[a4paper,12pt]{scrreprt}
\usepackage[utf8]{inputenc}
% Footer Package
\usepackage{scrpage2}
\pagestyle{scrheadings}
% Clear footer and header
\clearscrheadfoot
% Define \responsible and set footer
\makeatletter
\let\@responsible\relax
\def\responsible#1{\gdef\@responsible{#1}}
\ifoot{\@responsible}
\makeatother
% For test texts
\usepackage{blindtext}
\begin{document}
% Set author
\responsible{My Name}
\blindtext
% Does not work
\responsible{My Name 2}% \ignorespaces here or in the definition also does not work
\chapter{A Chapter}
\blindtext
\end{document}
文本/段落问题的 MWE:
\documentclass[a4paper,12pt]{scrreprt}
\usepackage[utf8]{inputenc}
% Footer Package
\usepackage{scrpage2}
\pagestyle{scrheadings}
% Clear footer and header
\clearscrheadfoot
% Define \responsible and set footer
\makeatletter
\let\@responsible\relax
\def\responsible#1{\gdef\@responsible{#1}}
\ifoot{\@responsible}
\makeatother
% For test texts
\usepackage{blindtext}
\begin{document}
% Set author
\responsible{My Name}
\blindtext\blindtext \blindtext
\blindtext
Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh
lectus placerat pede.
Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit.
Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing
elit. Duis fringilla tristique neque. Sed interdum libero ut metus. Pellentesque placerat.
Nam rutrum augue a leo. Morbi sed elit sit amet ante lobortis sollicitudin. Praesent
blandit blandit mauris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a,
turpis. Mauris lacinia lorem sit amet ipsum. Nunc quis urna dictum turpis accumsan
semper.
% Does not work
\responsible{My Name 2}% \ignorespaces here or in the definition also does not work
\blindtext
\end{document}
答案1
此方法使用everypage
将所有\responsible
s 连接到页脚中,并以逗号分隔列表的形式进行分隔。每次分页时,只有最后一次对 responsible 的调用才会保留,进入下一页。
为了满足 OP 的需求,我还引入了一个宏\Newresponsible{}
来清除记录,而不是延续之前的名字列表。
原始作者提到的问题之一是希望将\responsible{}
调用的激活延迟到下一段。此请求解决了当\responsible
在页面边界调用时,它会将名称应用于前一页而不是仅在新页面的问题。我认为我对这个问题的修复有点临时,需要两个部分。
我先排版 a
~\par
,然后排版为负数\vspace
,以撤消\baselineskip+\parskip
(应急警报!); 和我重新定义在下次调用时
\par
进行更改。\@responsible
\par
此 MWE 显示了几个特点。它\responsible
在第 1 页上提供了一个。然后,它\Newresponsible
在第 1/2 页边界处提供了一个,以显示第 1 页的负责内容未延续到第 2 页,并且第 2 页的负责内容未在第 1 页上预先列出。它还\responsibles
在第 2 页上提供了两个,以便人们可以看到只有最后一个\responsible
被延续到第 3 页。
\documentclass[a4paper,12pt]{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage{everypage}
% Footer Package
\usepackage{scrpage2}
\pagestyle{scrheadings}
% Clear footer and header
\clearscrheadfoot
% Define \responsible and set footer
\makeatletter
\def\@responsible{}
\let\svpar\par
\def\responsible#1{~\par\vspace{\dimexpr-\baselineskip-\parskip}%
\def\par{~%
\if\relax\@responsible\relax%
\xdef\@responsible{#1}%
\else%
\xdef\@responsible{\@responsible,\ #1}%
\fi
\xdef\@futureresponsible{#1}%
\let\par\svpar%
\svpar%
}%
}
\AddEverypageHook{%
\xdef\current@responsible{\@responsible}%
\ifoot{\current@responsible}%
\xdef\@responsible{\@futureresponsible}%
}
\def\Newresponsible#1{\responsible{#1}\AddThispageHook{\gdef\@responsible{}}}
\makeatother
% For test texts
\usepackage{blindtext}
\begin{document}
% Set author
\responsible{My Name}
\blindtext\blindtext \blindtext
\blindtext
Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh
lectus placerat pede.
Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit.
Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing
elit. Duis fringilla tristique neque. Sed interdum libero ut metus. Pellentesque placerat.
Nam rutrum augue a leo. Morbi sed elit sit amet ante lobortis sollicitudin. Praesent
blandit blandit mauris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a,
turpis. Mauris lacinia lorem sit amet ipsum. Nunc quis urna dictum turpis accumsan
semper.
\Newresponsible{Name 2}%
\blindtext
\responsible{Name 3}%
\blindtext[6]
\end{document}
第 1 页页脚:
第 2 页页脚:
第 3 页页脚:
答案2
唯一能确定您当前在哪个页面的方法是使用辅助文件。但是,要强制 \responsible 直到第二页才出现,您必须使用 \ignorespaces 或删除它和文本之间的 \par(空白行)。
\listfooter 可能可以与标准页脚包一起使用。我还没有尝试过。
\documentclass[a4paper,12pt]{scrreprt}
\usepackage[utf8]{inputenc}
% Footer Package
\pagestyle{empty}
\usepackage{everypage}
\makeatletter
\newcommand{\mylabel}[2]% #1 = label name, #2 = text for \ref
{\protected@write\@auxout{}{\string\newlabel{#1}{{#2}{\thepage}}}}
\newcounter{responsible}
\newcounter{irresponsible}
\setcounter{irresponsible}{1}
\newcommand{\smartfooter}%
{\raisebox{\dimexpr -\topmargin-\headheight-\headsep-\textheight-\footskip}[0pt][0pt]{\rlap{%
\ifodd\c@page\hspace{\oddsidemargin}\makebox[\textwidth][l]{\listfooter\hfill\thepage}%
\else\hspace{\evensidemargin}\makebox[\textwidth][l]{\thepage\hfill\listfooter}%
\fi}}
}%
\newcommand{\listfooter}{\ifnum\c@responsible=0\relax
\else
\ifnum\c@irresponsible=\c@responsible
\ref{author:\theresponsible}%
\else\ifnum\c@page=\pageref{author:\theresponsible}\relax
\stepcounter{irresponsible}%
\fi
\ref{author:\theirresponsible}%
\fi
\fi}
\AddEverypageHook{\smartfooter}
\makeatother
% Define \responsible and set footer
\newcommand*{\responsible}[1]{\pagebreak[3]\stepcounter{responsible}\mylabel{author:\theresponsible}{#1}}
% For test texts
\usepackage{blindtext}
\usepackage{everypage}
\begin{document}
% Set author
\responsible{My Name}
\blindtext\blindtext \blindtext
\blindtext
Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh
lectus placerat pede.
Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit.
Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing
elit. Duis fringilla tristique neque. Sed interdum libero ut metus. Pellentesque placerat.
Nam rutrum augue a leo. Morbi sed elit sit amet ante lobortis sollicitudin. Praesent
blandit blandit mauris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a,
turpis. Mauris lacinia lorem sit amet ipsum. Nunc quis urna dictum turpis accumsan
semper.
% Does not work
\responsible{My Name 2}% \ignorespaces here or in the definition also does not work
\blindtext
\end{document}