我不确定这是否可行,也不知道该如何实现,因此我不能包含 MWE。不过我猜想这类东西可用于创建 LaTeX 包。
这个想法是,只有当用户定义了变量时,才添加表格行(或任何文本)。如下所示:
% as part of a different file, say some sort of class I'm defining
\newcommand\address[1]{\def\theaddress{#1}}
\address{\theaddress{}} % I guess this defines an empty definition?
% later on the same document, inside a table (this is pseudo-code)
if \theaddress is not empty, write { Address & \theaddress \\ } else don't add anything
% later on the actual document I can define the address to whatever I want.
\address{This is my address}
更新
所以我想出了这个工作示例。假设您有以下用户文档:
\documentclass[]{project}
\contact{\footnotesize{John Doe}}
\contactb{\footnotesize{John Appleseed}}
\address{\footnotesize{PME}}
\addressb{\footnotesize{BME}}
\phone{\footnotesize{...}}
\phoneb{\footnotesize{...}}
\email{email1}
\emailb{email2}
% Reference top left
\begin{document}
\makeheader
\end{document}
还有一个 project.cls 如下:
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{tuletter}
\LoadClass[a4paper,10pt]{article}
\RequirePackage[absolute]{textpos} % positioning the header elements
\RequirePackage[usenames,dvipsnames]{color} % use gray color for default values and labels
% Font color for default values (values not defined in LaTeX file)
\newcommand{\tudefault}[1]{\textcolor{Gray}{#1}}
\newcommand\contact[1]{\def\tucontact{#1}}
\contact{\tudefault{contact name}}
\newcommand\contactb[1]{\def\tucontactb{#1}}
\contactb{\tudefault{contact name 2}}
\newcommand\address[1]{\def\tuaddress{#1}}
\address{\tuaddress{your address}}
\newcommand\addressb[1]{\def\tuaddressb{#1}}
\addressb{\tuaddressb{second line of your address}}
\newcommand\phone[1]{\def\tuphone{#1}}
\phone{\tudefault{+31 (0)15 278 \ldots}}
\newcommand\phoneb[1]{\def\tuphoneb{#1}}
\phoneb{\tudefault{+31 (0)15 278 \ldots}}
\newcommand\email[1]{\def\tuemail{#1}}
\email{\tudefault{\ldots @tudelft.nl}}
\newcommand\emailb[1]{\def\tuemailb{#1}}
\emailb{\tudefault{\ldots @tudelft.nl}}
%%% Labels
\newcommand{\turefval}[1]{\small{#1}}
\newcommand{\tureflab}[1]{\textcolor{Gray}{\scriptsize{#1}}}
\newcommand\tudatename{Date}
\newcommand\tucontactname{Contact}
\newcommand\tuaddressname{Your address}
\newcommand\tuphonename{Phone}
\newcommand\tuemailname{E-mail}
\newcommand{\makeheader}{%
\textblockorigin{0cm}{0cm}
% Reference
\begin{textblock*}{100mm}(25mm, 9mm)
\begin{tabular}{r@{ }cc}
\tureflab{\tucontactname} & \turefval{\tucontact} & \turefval{\tucontactb} \\
\tureflab{\tuaddressname} & \turefval{\tuaddress} & \turefval{\tuaddressb} \\
% & \turefval{\tuaddresss} & \turefval{\tuaddressbs} \\
\tureflab{\tuphonename} & \turefval{\tuphone} & \turefval{\tuphoneb}\\
\tureflab{\tuemailname} & \turefval{\tuemail} & \turefval{\tuemailb} \\
% \tureflab{\tusubjectname} & \turefval{\tusubject} \\
\end{tabular}
\end{textblock*}
}
\endinput
这是我从一个工作模板中剥离出来的。此代码的结果是:
但现在所有内容都是硬编码的,因此 2 个人会显示在顶部。我的想法是扩展此代码,使其只适用于 1 个人甚至更多人,同时保持相同的格式并允许每个字段包含更多信息(例如,如果地址需要占用更多空间以适合 2 行或更多行)。
答案1
\address{\theaddress{}} % I guess this defines an empty definition?
不,这定义了一个无限循环,因为它是
\def\theaddress{\theaddress{}}
所以\theaddress
有定义\theaddress{}
,所以如果你尝试扩展,\theaddress
它要做的第一件事就是尝试\theaddress
再次扩展,{}
在输入堆栈上向前推进,并且会一直这样做,直到用完堆栈。
使用初始设置
\def\theaddress{}
然后你可以使用
\ifx\theaddress\@empty\else
&Address& \theaddress\\
\fi
或者通常更好:根本不进行任何测试,只需使用
\theaddress
此时的初始定义是
\def\theaddress{}
所以它什么也不做,但是定义
\newcommand\address[1]{\def\theaddress{Address\\}}
因此如果\address
已经使用那么\theaddress
将形成完整的行。
避免将字体大小和其他内容放入文档中:
\documentclass[]{project}
\contact{John Doe}
\contact{John Appleseed}
\contact{Me}
\address{PME}
\address{BME}
\address{here}
\phone{...}
\phone{..}
\phone{123}
\email{email1}
\email{email2}
\email{me@there}
% Reference top left
\begin{document}
\makeheader
\end{document}
有了这个类
\LoadClass{article}
\let\contactrow\@gobble
\def\contact#1{%
\ifx\contactrow\@gobble\def\contactrow{Contact&}\fi
\expandafter\def\expandafter\contactrow\expandafter{\contactrow}}
\let\addressrow\@gobble
\def\address#1{%
\ifx\addressrow\@gobble\def\addressrow{Your Address&}\fi
\expandafter\def\expandafter\addressrow\expandafter{\addressrow}}
\let\phonerow\@gobble
\def\phone#1{%
\ifx\phonerow\@gobble\def\phonerow{Phone&}\fi
\expandafter\def\expandafter\phonerow\expandafter{\phonerow}}
\let\emailrow\@gobble
\def\email#1{%
\ifx\emailrow\@gobble\def\emailrow{E-mail&}\fi
\expandafter\def\expandafter\emailrow\expandafter{\emailrow}}
\def\makeheader{%
\begin{center}
\footnotesize
\begin{tabular}{r*{20}{l}}
\contactrow\\
\addressrow\\
\phonerow\\
\emailrow\\
\end{tabular}%
\end{center}}