更改命令中的预定义日期并保存为 datetime2 - 扩展问题?

更改命令中的预定义日期并保存为 datetime2 - 扩展问题?

格式中预定义了一个日期12 June 2015,我需要一个包datetime2来修改日期并将其翻译成其他语言。但是,它无法以以下方式工作:

\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[spanish,english]{babel}
\usepackage{xparse}
\usepackage{datetime2}
\DTMsetup{useregional}
\DTMsetstyle{ddmmyyyy}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\StrMonthToNum}{ m }
{
  \str_case:nnF { #1 } {
    { January   } { 01 }
    { February  } { 02 }
    { March     } { 03 }
    { April     } { 04 }
    { May       } { 05 }
    { June      } { 06 }
    { July      } { 07 }
    { August    } { 08 }
    { September } { 09 }
    { October   } { 10 }
    { November  } { 11 }
    { December  } { 12 }
  } { 12 } % Wrong month, defaults to December/12
}
\ExplSyntaxOff

% Change from `DD MMtext YYYY' to `YYYY-MM-DD'
\def\dtdateformat#1{\expandafter\dtghiredate#1\relax}
\def\dtghiredate#1 #2 #3\relax{#3-\StrMonthToNum{#2}-#1}

\newcommand{\originaldate}{12 June 2016}

\begin{document}
\selectlanguage{english}
\DTMsavedate{changeddate}{\dtdateformat{\originaldate}}
\DTMusedate{changeddate}

\selectlanguage{spanish}
\DTMusedate{changeddate}


\end{document}

它怎么可能起作用呢?

答案1

是的,这是一个扩展问题:\DTMsavedate不这样做。

您可以通过以下方式解决

\begingroup\edef\x{\endgroup
  \noexpand\DTMsavedate{changeddate}{\dtdateformat{\originaldate}}%
}\x

或者更简洁地,通过定义一个抽象:

\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[spanish,english]{babel}
\usepackage{xparse}
\usepackage{datetime2}
\DTMsetup{useregional}
\DTMsetstyle{ddmmyyyy}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\StrMonthToNum}{ m }
{
  \str_case:nnF { #1 } {
    { January   } { 01 }
    { February  } { 02 }
    { March     } { 03 }
    { April     } { 04 }
    { May       } { 05 }
    { June      } { 06 }
    { July      } { 07 }
    { August    } { 08 }
    { September } { 09 }
    { October   } { 10 }
    { November  } { 11 }
    { December  } { 12 }
  } { 12 } % Wrong month, defaults to December/12
}
\ExplSyntaxOff

% Change from `DD MMtext YYYY' to `YYYY-MM-DD'
\def\dtdateformat#1{\expandafter\dtghiredate#1\relax}
\def\dtghiredate#1 #2 #3\relax{#3-\StrMonthToNum{#2}-#1}
\def\eDTMsavedate#1#2{%
  \begingroup\edef\x{\endgroup
    \noexpand\DTMsavedate{#1}{#2}%
  }\x
}

\newcommand{\originaldate}{12 June 2016}

\begin{document}

\eDTMsavedate{changeddate}{\dtdateformat{\originaldate}}%
\DTMusedate{changeddate}

\selectlanguage{spanish}
\DTMusedate{changeddate}


\end{document}

在此处输入图片描述

相关内容