我必须在许多环境报告中生成影响和行动的摘要。有时分析中有两个或更多影响,所以我必须在表中手动创建行。有没有更智能的方法来创建这个表,而不需要定义很多新命令?
\documentclass{article}
\usepackage{etoolbox}
\usepackage{nameref}
\begin{document}
\section{Impacts on the Environment}
\subsection{Air quality}
\label{sec:air}
Analysis of air quality.
%conclusion
\newcommand{\ImpactAir}{Wood burning}
\newcommand{\CountermeasureAir}{Sleeve filter installation}
\textbf{Impact}: \ifstrempty{\ImpactAir}{Not applicable.}{\ImpactAir}
\textbf{Countermeasure}: \ifstrempty{\CountermeasureAir}{Not applicable.}{\CountermeasureAir}
\subsection{Water resources}
\label{sec:water}
Analysis of water resources.
%conclusion
\newcommand{\ImpactWater}{}
\newcommand{\CountermeasureWater}{}
\textbf{Impact}: \ifstrempty{\ImpactWater}{Not applicable.}{\ImpactWater}
\textbf{Countermeasure}: \ifstrempty{\CountermeasureWater}{Not applicable.}{\CountermeasureWater}
\subsection{Noise level}
\label{sec:noise}
Analysis of noise level.
%conclusion
\newcommand{\ImpactNoise}{High noise level}
\newcommand{\CountermeasureNoise}{Acoustic engine insulation}
\textbf{Impact}: \ifstrempty{\ImpactNoise}{Not applicable.}{\ImpactNoise}
\textbf{Countermeasure}: \ifstrempty{\CountermeasureNoise}{Not applicable.}{\CountermeasureNoise}\\
\section{Conclusion}
A succinct conclusion. See Table \ref{tab:sumary}.
\begin{table}[htb!]
\centering
\caption{Summary of impacts and countermeasures}
\label{tab:sumary}
\begin{tabular}{lll}
\hline
\textbf{Analyse} & \textbf{Impact} & \textbf{Countermeasure}\\
\hline
\nameref{sec:air} & \ifstrempty{\ImpactAir}{-}{\ImpactAir} & \ifstrempty{\CountermeasureAir}{-}{\CountermeasureAir}\\
\nameref{sec:water} & \ifstrempty{\ImpactWater}{-}{\ImpactWater} & \ifstrempty{\CountermeasureWater}{-}{\CountermeasureWater} \\
\nameref{sec:noise} & \ifstrempty{\ImpactNoise}{-}{\ImpactNoise} & \ifstrempty{\CountermeasureNoise}{-}{\CountermeasureNoise}\\
\hline
\end{tabular}
\end{table}
\end{document}
一个小细节,命令\ifstrempty
没有按预期工作。如何扩展它?
答案1
您可以使用expl3
的属性列表来为您存储数据,然后循环遍历属性列表中的项目来布置表格。
我定义了一个命令\DeclareImpact{<label>}{<impact>}{<countermeasure>}
,它替换了你的\newcommand{\Impact<label>}{<impact>}
和\newcommand{\Countermeasure<label>}{<countermeasure>}
。
<label>
定义后,您可以使用\impact{<label>}
和\countermeasure{<label>}
来检索存储的文本。如果从未定义,则返回<label>
后备文本(默认)。Not applicable
我还定义了一个\TableBody{<list-of-labels>}
命令,它接受一个<list-of-labels>
并根据给定的模板对表体进行排版\SetRowFormat
(的参数在中的\SetRowFormat
每个使用一次)。<label>
<list-of-labels>
\documentclass{article}
\usepackage{nameref}
\usepackage{booktabs}
\usepackage{xparse}
\ExplSyntaxOn
\prop_new:N \g__leonardo_impact_prop
\prop_new:N \g__leonardo_counter_prop
\makeatletter
\NewDocumentCommand \DeclareImpact { m +m +m }
{
\@bsphack
\prop_gput:Nnn \g__leonardo_impact_prop {#1} {#2}
\prop_gput:Nnn \g__leonardo_counter_prop {#1} {#3}
\@esphack
}
\makeatother
\NewExpandableDocumentCommand \impact { O{Not~applicable} m }
{
\prop_if_in:NnTF \g__leonardo_impact_prop {#2}
{ \prop_item:Nn \g__leonardo_impact_prop {#2} }
{ \exp_not:n {#1} }
}
\NewExpandableDocumentCommand \countermeasure { O{Not~applicable} m }
{
\prop_if_in:NnTF \g__leonardo_counter_prop {#2}
{ \prop_item:Nn \g__leonardo_counter_prop {#2} }
{ \exp_not:n {#1} }
}
%
\NewExpandableDocumentCommand \TableBody { m }
{ \clist_map_function:nN {#1} \__leonardo_table_row:n }
\NewDocumentCommand \SetRowFormat { m }
{ \cs_gset:Npn \__leonardo_table_row:n ##1 {#1} }
\cs_new_eq:NN \StrLowerCase \str_lowercase:n
\ExplSyntaxOff
\begin{document}
\section{Impacts on the Environment}
\subsection{Air quality}
\label{sec:air}
Analises of air quality.
\DeclareImpact{Air}{Wood burning}{Sleeve filter installation}
\textbf{Impact}: \impact{Air}
\textbf{Countermeasure}: \countermeasure{Air}
\subsection{Water resources}
\label{sec:water}
Analises of water resources.
\textbf{Impact}: \impact{Water}
\textbf{Countermeasure}: \countermeasure{Water}
\subsection{Noise level}
\label{sec:noise}
Analises of noise level.
\DeclareImpact{Noise}{High noise level}{Acoustic engine insulation}
\textbf{Impact}: \impact{Noise}
\textbf{Countermeasure}: \countermeasure{Noise}
\section{Conclusion}
A succinct conclusion. See Table \ref{tab:sumary}.
% Set table row format:
\SetRowFormat
{%
\nameref{sec:\StrLowerCase{#1}}
& \impact[--]{#1}
& \countermeasure[--]{#1} \\
}
\begin{table}[htb!]
\centering
\caption{Summary of impacts and countermeasures}
\label{tab:sumary}
\begin{tabular}{lll}
\toprule
\textbf{Analyse} & \textbf{Impact} & \textbf{Countermeasure}\\
\midrule
\TableBody{Air,Water,Noise}
\bottomrule
\end{tabular}
\end{table}
\end{document}