根据回答我收到了在页眉中打印简短标题\handoutheader
,我在一个文件中创建了宏,.sty
并将其用作我的会议演示文稿的模板。
\newcommand\@confname{} % create macro for name of conference
\newcommand\confname[1]{\renewcommand\@confname{#1}}
\newcommand\@confplace{} % create macro for place of conference
\newcommand\confplace[1]{\renewcommand\@confplace{#1}}
\newcommand\@confdate{} % create macro for date of conference
\newcommand\confdate[1]{\renewcommand\@confdate{#1}}
\newcommand{\handoutheader}{%
My name \hfill \@confname\\
My affiliation \hfill \@confplace\\
My e-mail address \hfill \@confdate\\} % make handout header
\AtBeginDocument{\setlength{\parindent}{0pt}\handoutheader}
逻辑是,我在.tex
讲义文件的序言中指定会议的名称、地点和日期,然后模板使用该信息在讲义上创建标题。
现在,我想在讲义文本中打印演讲日期。我该怎么做?我试过\confdate
和\@confdate
,但都不起作用。
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents}{mystyle.sty}
%% My package starts here
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mystyle}
\newcommand\@confname{} % create macro for name of conference
\newcommand\confname[1]{\renewcommand\@confname{#1}}
\newcommand\@confplace{} % create macro for place of conference
\newcommand\confplace[1]{\renewcommand\@confplace{#1}}
\newcommand\@confdate{} % create macro for date of conference
\newcommand\confdate[1]{\renewcommand\@confdate{#1}}
\newcommand{\handoutheader}{%
My name \hfill \@confname\\
My affiliation \hfill \@confplace\\
My e-mail address \hfill \@confdate\\} % make handout header
\AtBeginDocument{\setlength{\parindent}{0pt}\handoutheader}
\endinput
%% And ends here
\end{filecontents}
\usepackage{mystyle}
\confname{Name of conference}
\confplace{Place of conference}
\confdate{Date of conference}
\begin{document}
\vspace{2ex}\\
After hearing my talk, you will remember the date \confdate\ for the rest of your life.\\
After hearing my talk, you will remember the date \@confdate\ for the rest of your life.\\
\end{document}
答案1
在您的示例中,\confdate
接受一个参数。因此,当您使用
...remember the date \confdate\ for the rest of your life.
(控制空间\
)被抓取为参数并放置在里面\@confdate
。这就是为什么你的输出类似于
...一生都记住这个日期。
另一方面,当你使用
...remember the date \@confdate\ for the rest of your life.
解释是调用\@
,插入一个非常大的\spacefactor
- 剩下的就是confdate
。因此,输出是
...一生都记住这个日期。
理想情况下,你应该自己创建一个显示“特权内容”的宏。在下面的例子中,我创建了\printdate
设置\@confdate
- 保存日期的宏。
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents}{mystyle.sty}
%% My package starts here
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mystyle}
\newcommand\@confname{} % create macro for name of conference
\newcommand\confname[1]{\renewcommand\@confname{#1}}
\newcommand\@confplace{} % create macro for place of conference
\newcommand\confplace[1]{\renewcommand\@confplace{#1}}
\newcommand\@confdate{} % create macro for date of conference
\newcommand\confdate[1]{\renewcommand\@confdate{#1}}
\newcommand{\printdate}{\@confdate}
\newcommand{\handoutheader}{%
My name \hfill \@confname\\
My affiliation \hfill \@confplace\\
My date \hfill \@confdate\\} % make handout header
\AtBeginDocument{\setlength{\parindent}{0pt}\handoutheader}
\endinput
%% And ends here
\end{filecontents}
\usepackage{mystyle}
\confname{Name of conference}
\confplace{Place of conference}
\confdate{Date of conference}
\begin{document}
\bigskip
After hearing my talk, you will remember the date \printdate{} for the rest of your life.
\end{document}
答案2
两个主要问题是:
要在宏中使用
@
,您需要使用\makeatletter
和\makeatother
。请参阅下面的参考资料。定义
\newcommand\confdate[1]{\renewcommand\@confdate{#1}}
意味着调用将
\confdate
的值设置\@confdate
为第一个参数。在您的代码中,第一行(在下面的 MWE 中被注释掉)将 的值重置\@confdate
为空白,然后在对 的后续调用中将其打印出来\@confdate
。注释掉该调用可以解决问题。
笔记:
- 我建议你不要
\\
在新行的末尾使用。只需留一个空行来表示新的段落分隔符。 - 如果您不想使用,
\makeatletter
就不要使用其中包含的宏名称@
,或者定义如\ShowConfDate
MWE 中所示的宏。
参考
代码:
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents}{mystyle.sty}
%% My package starts here
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mystyle}
\newcommand\@confname{} % create macro for name of conference
\newcommand\confname[1]{\renewcommand\@confname{#1}}
\newcommand\@confplace{} % create macro for place of conference
\newcommand\confplace[1]{\renewcommand\@confplace{#1}}
\newcommand\@confdate{} % create macro for date of conference
\newcommand\confdate[1]{\renewcommand\@confdate{#1}}
\newcommand\ShowConfDate{\@confdate}%
\newcommand{\handoutheader}{%
My name \hfill \@confname\\
My affiliation \hfill \@confplace\\
My e-mail address \hfill \@confdate\\} % make handout header
\AtBeginDocument{\setlength{\parindent}{0pt}\handoutheader}
\endinput
%% And ends here
\end{filecontents}
\usepackage{mystyle}
\confname{Name of conference}
\confplace{Place of conference}
\confdate{Date of conference}
\begin{document}
\makeatletter
%After hearing my talk, you will remember the date \confdate\ for the rest of your life.
After hearing my talk, you will remember the date \@confdate\ for the rest of your life.
\makeatother
\medskip
If you don't want to be using \verb|\makeatletter| you can use \verb|\ShowConfDate| as in:
``The date is \ShowConfDate."
\end{document}
答案3
如果您不需要为宏的打印版本分配新名称\confname
,您可以定义此类宏的更智能版本:如果等号紧随其后,则存储该值,否则使用该值。例如:
The test: \confname\ % value is used, empty value is printed as default.
\confname={Name} % the value "Name" is stored.
The second test: \confname\ % the value "Name" is printed.
实现如下:
\def\confname{\futurelet\next\confnameA}
\def\confnameA{\ifx\next=\expandafter\confnameB \else \confnameC\fi}
\def\confnameB=#1{\def\confnameC{#1}}
\def\confnameC{}
但是我们懒得把这段代码写三遍(for 、、\confname
)。因此,我们可以创建一个更智能的宏:\confplace
\confdate
\def\sdef#1{\expandafter\def\csname#1\endcsname}
\def\setmacro#1{%
\sdef{#1}{\expandafter\futurelet\expandafter\next\csname#1.A\endcsname}%
\sdef{#1.A}{\ifx\next=\csname#1.B\expandafter\endcsname \else \csname#1.C\endcsname\fi}%
\sdef{#1.B}=##1{\sdef{#1.C}{##1}}%
}
\setmacro{confname} \setmacro{confplace} \setmacro{confdate}
您可以将此代码插入到您的.sty
文件中\newcommand\@confname...\newcommand\confdate
,并且可以在宏主体中将etc. 替换\@confname
为etc. 。当然,用法略有不同:\confname
\handoutheader
\confname={Name of conference}
\confplace={Place of conference}
\confdate={Date of conference}