![如何使用 pgfkeys 设置嵌套键](https://linux22.com/image/279379/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20pgfkeys%20%E8%AE%BE%E7%BD%AE%E5%B5%8C%E5%A5%97%E9%94%AE.png)
我有几个文档,希望能够根据演示内容以不同的样式进行格式化。内容应该保持不变,但格式可能需要大幅改变。我希望能够将内容存储在键值中,以便能够更灵活地组织格式。
这是一个相对较小的 MWE,但它却阐明了其基本概念:
\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage[margin=0.5in]{geometry}
\usepackage{pgfkeys}
\usepackage{etoolbox}
\usepackage{booktabs}
\usepackage{array}
\makeatletter
%-@-(1)---------------------------------------------------------------------
%% Inner workings which the user should not have to touch
%% assuming that I've set this up correctly.
\pgfkeys{/ae/present/info/.cd,
presentation style/.store in=\ae@style,
resource/.code=\ae@resource@code{#1},
}
\pgfkeys{/ae/resource/code/.cd,
name/.store in=\ae@name,
information/.code=\ae@information@code{#1},
}
\pgfkeys{/ae/information/code/.cd,
given/.store in=\ae@given,
aka/.store in=\ae@aka,
conclusion/.store in=\ae@conclusion,
}
\long\def\presentinfo#1{\pgfkeys{/ae/present/info/.cd,#1}}
\long\def\ae@resource@code#1{%%
\pgfkeys{/ae/resource/code/.cd,#1}
\csname ae@finalize@formatting@\ae@style\endcsname
}
\long\def\ae@information@code#1{%%
\bgroup
\pgfkeys{/ae/information/code/.cd,#1}
\csname ae@build@presentation@\ae@style\endcsname
\egroup
}
%% Internal macros for formatting content
\def\ae@style{A}
\def\ae@name{}
\def\ae@given{}
\def\ae@aka{}
\def\ae@empty{}
\def\ae@conclusion{}
\def\ae@content@body{}
%% why do I have to "hide" \midrule like this?
\def\ae@midrule{\midrule}
%-@-(2)---------------------------------------------------------------------
%% User can play with the following macros to change the formatting
%% of the output.
%%--------------------------------------------------------------------------
%% STYLE="A"
%%--------------------------------------------------------------------------
\def\ae@build@presentation@A{%%
\xdef\ae@content@body{\expandonce\ae@content@body\noexpand\\
\noexpand\ae@midrule
\expandonce\ae@given \noexpand &
\expandonce\ae@aka \noexpand &
\expandonce\ae@conclusion }}
\def\ae@finalize@formatting@A{%%
\par\vspace{2ex}%%
\noindent
\textbf{\ae@name:}\par\vspace{2ex}
\begin{tabular}{>{\raggedright}p{3in}cp{3in}}
\textsf{Given} & \hspace{3em} & \textsf{Conclusion}
\ae@content@body
\end{tabular}\par\vspace{1ex}
\gdef\ae@content@body{}%%
}
%%--------------------------------------------------------------------------
%% STYLE="B"
%%--------------------------------------------------------------------------
\long\def\ae@build@presentation@B{%%
\xdef\ae@content@body{\expandonce\ae@content@body
\noexpand\item
\noexpand\textsf{Given:}
\noexpand\parbox[t]{\noexpand\dimexpr\noexpand\linewidth-2.5cm}{\noexpand\raggedright\expandonce\ae@given}
\noexpand\hspace*{\noexpand\fill}%%
\ifx\ae@aka\ae@empty\else$\rightarrow$\fi
\noexpand\textsf{\ae@aka}
\noexpand\par
\noexpand\textsf{Conclude:} \expandonce\ae@conclusion
}}
\long\def\ae@finalize@formatting@B{%%
\par
\noindent
$\left.
\begin{minipage}{4in}
\begin{itemize}
\ae@content@body
\end{itemize}
\end{minipage} \right\}${\large if working with \sffamily\ae@name}\par \vspace{4ex}
\def\ae@content@body{}%%
}
\makeatother
\begin{document}
\presentinfo{%%'
presentation style=A,
resource={%%
name=Triangles,
information={%%'
given=Three pairs of sides are congruent,
aka=SSS,
conclusion=The two triangles are congruent.},
information={
given=Two pairs of sides are congruent and 1 pair of included angles is congruent,
aka=SAS,
conclusion=The two triangles are congruent,}
},
resource={%%
name=Transversal,
information={%%
given=Alternate interior angles are congruent,
conclusion=The two lines are parallel.},
information={%%
given=Same side interior angles are supplementary,
conclusion=The two lines are parallel.},
}
}
\end{document}
使用时presentation style=A
,生成的文档显示为
使用时presentation style=B
,生成的文档显示为
这让我在文档格式方面有了很大的自由度。但我的方法似乎有点太复杂了。
有没有更好的方法?我想继续使用pgfkeys
。有没有办法pgfkeys
避免嵌套调用进一步的键定义?我尝试创建一个.style
可以实现我想要的,但没有任何进展。
和我的主要问题有点相关的是:我在\midrule
操作上遇到了问题。我不得不把它埋在另一个宏里。否则,即使\noexpand\midrule
我这样做,也会得到对齐错误。有什么想法吗?