根据我上次的评论问题我尝试使用 创建新命令\NewDocumentCommand
。但是,它无法识别某些可选参数 - 它们只是按原样打印。知道原因和解决方法吗?
最小代码示例:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage{tabularx} % Til tabeller der tilpaser sig i bredden
\newcolumntype{R}{>{\raggedleft\arraybackslash}r}
\usepackage{xifthen} % Used to test if an argument is empty
\usepackage{datetime} % Used to format dates
\newdateformat{timeLineDate}{\THEDAY.~\shortmonthname~\THEYEAR}
\newdateformat{monthYearDate}{\monthname[\THEMONTH]~\THEYEAR}
\usepackage{xparse}
\makeatletter
\newcommand*{\textlabel}[2]{%
\edef\@currentlabel{#1}% Set target label
\phantomsection% Correct hyper reference link
#1\label{#2}% Print and store label
}
\makeatother
\NewDocumentCommand\createPersonNew{omomd<>o}
{
\begin{tabularx}{\textwidth}{lXRc}%
Navn: & #2 & #4 & $\Re$\\
\IfNoValueTF{#1#3}{}{&&\IfNoValueTF{#5}{\IfNoValueTF{#6}{}{#6 & \cross}}{#5 & $\star$ \IfNoValueTF{#6}{}{\\&& #6 & \cross}}}
%...
\end{tabularx}%
}
%Arguments
% 1) The argument to test
% 2) If empty then do this
% 3) If not empty then do this
\newcommand{\ifNoArgumentElse}[3]{
\ifthenelse{\isempty{#1}}{#2}{#3}%
}
\newcommand{\cross}[1][1pt]{\ooalign{%
\rule[1ex]{1ex}{#1}\cr% Horizontal bar
\hss\rule{#1}{.7em}\hss\cr}% Vertical bar
}
\newcommand{\printLongDate}[3]{%
\ifNoArgumentElse{#1#2#3}{}{%
\ifNoArgumentElse{#1}{%
\ifNoArgumentElse{#2}{#3}{\monthYearDate\formatdate{1}{#2}{#3}}%
}{\formatdate{#1}{#2}{#3}}%
}%
}
\begin{document}
\subsection{Else Jensen}
\label{person1}
\createPersonNew
[else]
{Else1940}
[Else Jensen]
{340}
[\printLongDate{12}{01}{1963}]
<\printLongDate{24}{09}{1979}>
\end{document}
答案1
检测到一些错误:
- 参数 5 和 6 的接口被误用和交换
\IfNoValueTF{#1#3} is wrong -- the
\IfNoValueTF` 宏仅适用于一个可选参数,不适用于它们的组合- 参数之间必须有换行符!
此外,我还“进步”了\printLongDate
一点
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage{tabularx} % Til tabeller der tilpaser sig i bredden
\newcolumntype{R}{>{\raggedleft\arraybackslash}r}
\usepackage{etoolbox}
\usepackage{xifthen} % Used to test if an argument is empty
\usepackage{datetime} % Used to format dates
\newdateformat{timeLineDate}{\THEDAY.~\shortmonthname~\THEYEAR}
\newdateformat{monthYearDate}{\monthname[\THEMONTH]~\THEYEAR}
\usepackage{xparse}
\makeatletter
\newcommand*{\textlabel}[2]{%
\edef\@currentlabel{#1}% Set target label
\phantomsection% Correct hyper reference link
#1\label{#2}% Print and store label
}
\makeatother
\NewDocumentCommand\createPersonNew{omomo+d<>}
{%
\begin{tabularx}{\textwidth}{lXRc}%
Navn: & #2 & #4 & $\Re$\\
\IfNoValueTF{#1}{}{&&\IfNoValueTF{#5}{\IfNoValueTF{#6}{}{#6 & \cross}}{#5 & $\star$ \IfNoValueTF{#6}{}{\\&& #6 & \cross}}}
% ...
\end{tabularx}%
}
%Arguments
% 1) The argument to test
% 2) If empty then do this
% 3) If not empty then do this
\newcommand{\ifNoArgumentElse}[3]{
\ifthenelse{\isempty{#1}}{#2}{#3}%
}
\newcommand{\cross}[1][1pt]{\ooalign{%
\rule[1ex]{1ex}{#1}\cr% Horizontal bar
\hss\rule{#1}{.7em}\hss\cr}% Vertical bar
}
\NewDocumentCommand{\printLongDate}{od<>o}{%
\IfNoValueTF{#1}{%
\IfNoValueTF{#2}{#3}{%
\protect\monthYearDate\formatdate{1}{#2}{#3}
}%
}{%
\IfNoValueTF{#2}{#3}{%
\protect\formatdate{#1}{#2}{#3}%
}%
}%
}
\begin{document}
\subsection{Else Jensen}
\label{person1}
\createPersonNew[else]{Else1940}[Else Jensen]{340}[\printLongDate[12]<01>[1963]]<
\printLongDate[24]<09>[1979]>
\end{document}