Pasar el valor del comando devuelto a otra función

Pasar el valor del comando devuelto a otra función

Estoy intentando pasar un valor de un comando a otra función sin suerte. Por ejemplo, tengo algunos datos \DTLloaddb[noheader, keys={key,value}]{definitions}{\data.def}a los que accedo con \DTLfetch{definitions}{key}{MyFromDate}{value}.

Ahora quiero pasar este valor de data.def(por ejemplo, 1990-10-09) a \printdate{}para poder formatear esta fecha en el formato preferido de mi idioma:9 de octubre de 1990.

Pensé que podría pasar fácilmente mi valor devuelto que está almacenado \DTLfetch{}sin \printdate{}éxito: \printdate{\DTLfetch{definitions}{key}{MyFromDate}{value}}arrojará un error como: ! Illegal parameter number in definition of \iso@date.pero llamar \DTLfetch{definitions}{key}{MyFromDate}solo imprimirá el valor de fecha sin formato (sin formato) sin ningún problema.

Desafortunadamente, no pude encontrar ningún recurso de ayuda sobre cómo pasar valores de función a otra función. Me encantaría que alguien pudiera proporcionarme información adicional sobre este tema. ¡Gracias!

Respuesta1

Falta un ejemplo mínimo (no) funcional que muestre qué paquetes/clase de documento utiliza y de qué manera se llaman las macros.

Supongo que \DTLfetchviene delherramienta de datospaquete y \printdateviene delisodarpaquete.


Por un lado, el comando \DTLfetchen sí no es un comando totalmente ampliable. Eso significa que las asignaciones (temporales) y similares se llevan a cabo cada vez que se llama ese comando.

Por otro lado, \printdateespera que su argumento contenga sólo elementos que

  • cualquiera de los dos es completamente expandible y cuya expansión en algún momento produce solo tokens que pertenecen a la secuencia de caracteres que forman la fecha
  • o pertenecer a la secuencia de caracteres que forman la fecha.

Pero el comando \DTLfetchno solo imprime/devuelve un valor, sino que también guarda ese valor en la macro \dtlcurrentvalue.
\dtlcurrentvaluea su vez es totalmente ampliable.

Por lo tanto, inmediatamente después de llamar, \DTLfetchpuede usar la macro \dtlcurrentvaluedentro del argumento de \printdate.

Como se acaba de decir, \DTLfetchtambién imprime el valor devuelto.

Si no desea que se imprima el valor, no use el comando \DTLfetch, sino el comando \DTLgetvalueforkeyque, dentro de otro argumento, le permite especificar el nombre de una macro que se (re)definirá para expandirse al valor deseado. valor de retorno.

\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}

ingrese la descripción de la imagen aquí


Desea que la fecha tenga el formato:
<name of the month><non-break-space><day>,<space><year>.

No encontré una manera de lograr esto fácilmente usando elisodarpaquete.
Por lo tanto sugiero no usar elisodarpaquete pero usando los paquetesfechahora2yfechahora2-calcque provienen del autor que también escribió elherramienta de datospaquete,Dra. Nicola Talbot.

Usando estos paquetes puedes definir fácilmente nuevos estilos de fechas según tus gustos:

\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}

ingrese la descripción de la imagen aquí

información relacionada