![为大型多行文本创建宏](https://linux22.com/image/339946/%E4%B8%BA%E5%A4%A7%E5%9E%8B%E5%A4%9A%E8%A1%8C%E6%96%87%E6%9C%AC%E5%88%9B%E5%BB%BA%E5%AE%8F.png)
我是 LaTeX 新手,刚刚学会了如何制作宏,并用它解决了一个问题。请让我描述一下这个问题和我的解决方案。
这是我的问题。
\documentclass{article}
\begin{document}
% This is just one example of multiple such sections that would be
% present in this document.
% ----- Begin Example -----
% Company Name
\section*{FooCorp}
% Founded Date
\textbf{Founded 2013} \\
% Domain
\textit{Cross Platform Software} \\
% Location
\textit{Singapore}
% Short Introduction
FooCorp creates great products in Foo area.
FooCorp believes in uniformity and ease of use.
% Detailed Points
\begin{itemize}
\item FooCorp supports both Unix and Windows users. It conforms to
existing Unix standards and bundles software with typical Windows
installers.
\item FooCorp provides you end-to-end support regarding any problems
encountered from the inception stage to go-live stage.
\end{itemize}
% Customers
Customers: Existing list of customers of FooCorp includes BarCorp,
BazCorp, QuxCorp.
% ----- End Example -----
% Another section similar to the above one would begin here. There would
% be multiple such sections in this document.
\end{document}
上述文档应该包含多个部分,每个部分都包含公司简介。为了简洁起见,我在这里只显示了一个部分。
现在我想将数据与展示分离,例如,如果将来我决定“成立”日期应为斜体,或者“客户”列表应位于“详细要点”之前,我不必去编辑每个部分。我应该能够在一个地方编辑这些风格决定,并且所有部分都应根据它进行呈现。
所以我用这样的宏解决了它。
\documentclass{article}
\newcommand{\corp}[6]{
\section*{#1}
\textbf{Founded #2} \\
\textit{#3} \\
\textit{#4}
#5
Customers: Existing list of customers of #1 includes #6.
}
\begin{document}
% This is just one example of multiple such sections that would be
% present in this document.
% ----- Begin Example -----
\corp{FooCorp}
{2013}
{Cross Platform Software}
{Singapore}
{
% Short Introduction
FooCorp creates great products in Foo area.
FooCorp believes in uniformity and ease of use.
% Detailed Points
\begin{itemize}
\item FooCorp supports both Unix and Windows users. It conforms to
existing Unix standards and bundles software with typical Windows
installers.
\item FooCorp provides you end-to-end support regarding any problems
encountered from the inception stage to go-live stage.
\end{itemize}
}
{
% Customers
BarCorp, BazCorp, QuxCorp
}
% ----- End Example -----
% Another section similar to the above one would begin here. There would
% be multiple such sections in this document.
\end{document}
但作为新手,我觉得它非常不像 LaTeX(如果有这种东西的话)。奇怪的是,宏的一些参数是多行参数。
我的问题:
- 这真的是使用宏可以解决的正确问题吗?
- 我需要注意这种方法有哪些缺点?
- 您将如何解决这一问题?
答案1
一些改进建议,使用空行代替\\
并应用键值接口。使用键的优点是指定它们的顺序并不重要!
(我知道,常见的抱怨xkeyval
会出现 ;-))
必须意识到未设置的键要么未定义,要么再次采用旧值,这可以通过分组来防止!
\documentclass{article}
\usepackage{xkeyval}
\makeatletter
\define@key{corp}{sectionname}{\def\KVcorpsection{#1}}
\define@key{corp}{founded}{\def\KVcorpfounded{#1}}
\define@key{corp}{info}{\def\KVcorpinfo{#1}}
\define@key{corp}{otherinfo}{\def\KVcorpotherinfo{#1}}
\define@key{corp}{customers}{\def\KVcorpcustomers{#1}}
\makeatother
\newcommand{\corp}[2][]{%
\begingroup
\setkeys{corp}{#1}
\section*{\KVcorpsection}
\textbf{Founded \KVcorpfounded}
\textit{\KVcorpinfo}
\textit{\KVcorpotherinfo}
#2%
Customers: Existing list of customers of \KVcorpsection includes \KVcorpcustomers.
\par
\endgroup
}
\begin{document}
% This is just one example of multiple such sections that would be
% present in this document.
% ----- Begin Example -----
\corp[sectionname=FooCorp, customers={BarCorp, BazCorp, QuxCorp},info={Cross Platform Software}, otherinfo={Singapore}, founded={2013}]
{% Short Introduction
FooCorp creates great products in Foo area.
FooCorp believes in uniformity and ease of use.
% Detailed Points
\begin{itemize}
\item FooCorp supports both Unix and Windows users. It conforms to
existing Unix standards and bundles software with typical Windows
installers.
\item FooCorp provides you end-to-end support regarding any problems
encountered from the inception stage to go-live stage.
\end{itemize}
}
% ----- End Example -----
% Another section similar to the above one would begin here. There would
% be multiple such sections in this document.
\end{document}