將傳回的命令值傳遞給另一個函數

將傳回的命令值傳遞給另一個函數

我試圖將命令的值傳遞給另一個函數,但沒有運氣。例如,我有一些可以\DTLloaddb[noheader, keys={key,value}]{definitions}{\data.def}使用 存取的資料\DTLfetch{definitions}{key}{MyFromDate}{value}

現在我想將此值從data.def(例如 1990-10-09)傳遞到 ,以便\printdate{}我可以將此日期格式化為我的語言首選格式:1990 年 10 月 9 日

我以為我可以輕鬆地傳遞儲存在\DTLfetch{}to中的返回值,\printdate{}但沒有成功:\printdate{\DTLfetch{definitions}{key}{MyFromDate}{value}}會拋出如下錯誤:! Illegal parameter number in definition of \iso@date.\DTLfetch{definitions}{key}{MyFromDate}單獨調用將列印未格式化的(原始)日期值,沒有任何問題。

不幸的是,我找不到任何有關將函數值傳遞到另一個函數的幫助資源。如果有人能為我提供一些有關該主題的附加信息,我將很高興。謝謝!

答案1

缺少展示您使用的套件/文檔類別以及呼叫巨集的方式的最小(非)工作範例。

我假設\DTLfetch來自數據工具包並\printdate來自異索達包裹。


一方面,該命令\DTLfetch本身並不是一個完全可擴展的命令。這意味著每當調用該命令時都會執行(臨時)分配等。

另一方面,\printdate期望其參數僅包含以下標記:

  • 兩者都是完全可擴展的,並且其在某個階段的擴展僅產生屬於形成日期的字元序列的標記
  • 或屬於形成日期的字元序列。

但該命令\DTLfetch不僅僅列印/返回一個值,它還將該值保存到巨集中\dtlcurrentvalue
\dtlcurrentvalue反過來又是完全可擴展的。

因此,在呼叫後,您可以在 的參數中\DTLfetch使用該巨集。\dtlcurrentvalue\printdate

正如剛才所說,\DTLfetch也列印返回值。

如果您不希望列印該值,請不要使用該命令\DTLfetch,而是使用該命令\DTLgetvalueforkey(在另一個參數中),該命令允許您指定要(重新)定義以擴展為所需的巨集的名稱返回值。

\documentclass[english]{article}
\usepackage{datatool}
\usepackage[num]{isodate}
\usepackage{filecontents}

\begin{filecontents*}{MyDataFile.def}
WhatsoeverKeyA,WhatsoeverValueA
WhatsoeverKeyB,WhatsoeverValueB
MyFromDate,1990-10-09
WhatsoeverKeyC,WhatsoeverValueC
\end{filecontents*}


\newcommand\data{MyDataFile}
\DTLloaddb[noheader, keys={key,value}]{definitions}{\data.def}

\begin{document}

This does both print the return-value and save the return-value to
\verb|\dtlcurrentvalue|:

\verb|\DTLfetch{definitions}{key}{MyFromDate}{value}|:
\DTLfetch{definitions}{key}{MyFromDate}{value}\\
\begingroup\footnotesize
\verb|% use the database "definitions".|\\
\verb|% use the column whose name is "value".|\\
\verb|% use the row which in the column "key" holds the item "MyFromData".|
\endgroup

\verb|\dtlcurrentvalue| yields: \dtlcurrentvalue

Now you can use \verb|\dtlcurrentvalue| within the argument of 
\verb|\printdate|.

\verb|\printdate{\dtlcurrentvalue}| yields: \printdate{\dtlcurrentvalue}

\hrulefill

This does just save the return-value to the macro \verb|\MyCommand|:


\verb|\DTLgetvalueforkey{\MyCommand}{value}{definitions}{key}{MyFromDate}|\\
\begingroup\footnotesize
\verb|% define the macro "\MyCommand".|\\
\verb|% use the database "definitions".|\\
\verb|% use the column whose name is "value".|\\
\verb|% use the row which in the column "key" holds the item "MyFromData".|
\endgroup

\DTLgetvalueforkey{\MyCommand}{value}{definitions}{key}{MyFromDate}        

\verb|\MyCommand| yields: \MyCommand

Now you can use \verb|\MyCommand| within the argument of
\verb|\printdate|.

\verb|\printdate{\MyCommand}| yields: \printdate{\MyCommand}

\end{document}

在此輸入影像描述


您希望將日期格式設為:
<name of the month><non-break-space><day>,<space><year>

我沒有找到一種方法可以輕鬆使用異索達包裹。
因此我建議不要使用異索達包但使用包日期時間2datetime2 計算來自作者,他也寫過數據工具包裹,尼古拉·塔爾博特博士

使用這些包,您可以根據自己的喜好輕鬆定義新的日期樣式:

\documentclass[english]{article}
\usepackage{datatool, datetime2, datetime2-calc}
\usepackage{filecontents}

\DTMnewdatestyle{MyDatestyle}{% definitions
  % the \number does remove leading zeros from the "day"-element of the date.
  \renewcommand*{\DTMdisplaydate}[4]{\DTMmonthname{##2}~\number##3, ##1}%
  \renewcommand*{\DTMDisplaydate}{\DTMdisplaydate}%
}

\begin{filecontents*}{MyDataFile.def}
WhatsoeverKeyA,WhatsoeverValueA
WhatsoeverKeyB,WhatsoeverValueB
MyFromDate,1990-10-09
WhatsoeverKeyC,WhatsoeverValueC
\end{filecontents*}

\newcommand\data{MyDataFile}
\DTLloaddb[noheader, keys={key,value}]{definitions}{\data.def}

\begin{document}

A new date-style can be defined using \textbf{datetime2 and datetime2-calc}:
\begin{verbatim}
\DTMnewdatestyle{MyDatestyle}{% definitions
  % the \number does remove leading zeros from the "day"-element of the date.
  \renewcommand*{\DTMdisplaydate}[4]{\DTMmonthname{##2}~\number##3, ##1}%
  \renewcommand*{\DTMDisplaydate}{\DTMdisplaydate}%
}
\end{verbatim}

\hrulefill

This does both print the return-value and save the return-value to
\verb|\dtlcurrentvalue|:

\verb|\DTLfetch{definitions}{key}{MyFromDate}{value}|:
\DTLfetch{definitions}{key}{MyFromDate}{value}\\
\begingroup\footnotesize
\verb|% use the database "definitions".|\\
\verb|% use the column whose name is "value".|\\
\verb|% use the row which in the column "key" holds the item "MyFromData".|
\endgroup

\verb|\dtlcurrentvalue| yields: \dtlcurrentvalue

Now you can use \verb|\dtlcurrentvalue| within the argument of
\verb|\DTMdate| and \verb|\DTMDate|.
\begin{verbatim}
\begingroup
\DTMsetdatestyle{MyDatestyle}
\DTMdate{\dtlcurrentvalue}

\DTMDate{\dtlcurrentvalue}
\endgroup
\end{verbatim}%
yields:

\begingroup
\DTMsetdatestyle{MyDatestyle}
\DTMdate{\dtlcurrentvalue}

\DTMDate{\dtlcurrentvalue}
\endgroup

\hrulefill

This does just save the return-value to the macro \verb|\MyCommand|:

\verb|\DTLgetvalueforkey{\MyCommand}{value}{definitions}{key}{MyFromDate}|\\
\begingroup\footnotesize
\verb|% define the macro "\MyCommand".|\\
\verb|% use the database "definitions".|\\
\verb|% use the column whose name is "value".|\\
\verb|% use the row which in the column "key" holds the item "MyFromData".|
\endgroup

\DTLgetvalueforkey{\MyCommand}{value}{definitions}{key}{MyFromDate}

\verb|\MyCommand| yields: \MyCommand


Now you can use \verb|\MyCommand| within the argument of
\verb|\DTMdate| and \verb|\DTMDate|.
\begin{verbatim}
\begingroup
\DTMsetdatestyle{MyDatestyle}
\DTMdate{\MyCommand}

\DTMDate{\MyCommand}
\endgroup
\end{verbatim}%
yields:

\begingroup
\DTMsetdatestyle{MyDatestyle}
\DTMdate{\MyCommand}

\DTMDate{\MyCommand}
\endgroup

\end{document}

在此輸入影像描述

相關內容