Passando o valor do comando retornado para outra função

Passando o valor do comando retornado para outra função

Estou tentando passar o valor de um comando para outra função sem sorte. Por exemplo, tenho alguns dados \DTLloaddb[noheader, keys={key,value}]{definitions}{\data.def}aos quais acesso com \DTLfetch{definitions}{key}{MyFromDate}{value}.

Agora quero passar esse valor de data.def(por exemplo, 1990/10/09) para \printdate{}para poder formatar essa data no formato de minha preferência de idioma:9 de outubro de 1990.

Achei que poderia facilmente passar meu valor retornado, que está armazenado \DTLfetch{}sem \printdate{}sucesso: \printdate{\DTLfetch{definitions}{key}{MyFromDate}{value}}gerará um erro como: ! Illegal parameter number in definition of \iso@date.mas chamar \DTLfetch{definitions}{key}{MyFromDate}sozinho imprimirá o valor de data não formatado (bruto) sem nenhum problema.

Infelizmente não consegui encontrar nenhum recurso de ajuda relacionado à passagem de valores de função para outra função. Eu ficaria feliz se alguém pudesse me fornecer algumas informações adicionais sobre este tópico. Obrigado!

Responder1

Está faltando um exemplo mínimo (não) funcional exibindo quais pacotes/classes de documentos você usa e de que maneira as macros são chamadas.

Presumo que \DTLfetchvenha doferramenta de dadospacote e \printdatevem doisodatopacote.


Por um lado, o comando \DTLfetchem si não é um comando totalmente expansível. Isso significa que atribuições (temporárias) e similares são executadas sempre que esse comando é chamado.

Por outro lado, \printdateespera que seu argumento contenha apenas tokens que

  • ambos são totalmente expansíveis e cuja expansão em algum estágio produz apenas tokens pertencentes à sequência de caracteres que forma a data
  • ou pertencem à sequência de caracteres que forma a data.

Mas o comando \DTLfetchnão apenas imprime/retorna um valor, ele também salva esse valor na macro \dtlcurrentvalue.
\dtlcurrentvaluepor sua vez, é totalmente expansível.

Portanto logo após chamar \DTLfetchvocê pode usar a macro \dtlcurrentvaluedentro do argumento de \printdate.

Como acabamos de dizer, \DTLfetchtambém imprime o valor retornado.

Se você não deseja que o valor seja impresso, não use o comando, \DTLfetchmas sim o comando \DTLgetvalueforkeyque - dentro de outro argumento - permite especificar o nome de uma macro que deve ser (re)definida para expandir para o desejado 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}

insira a descrição da imagem aqui


Você deseja que a data seja formatada como:
<name of the month><non-break-space><day>,<space><year>.

Não encontrei uma maneira de fazer isso facilmente usando oisodatopacote.
Portanto, sugiro não usar oisodatopacote, mas usando os pacotesdatahora2edatahora2-calcque vêm do autor que também escreveu oferramenta de dadospacote,Dra. Nicola Talbot.

Usando esses pacotes você pode definir facilmente novos estilos de data de acordo com suas preferências:

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

insira a descrição da imagem aqui

informação relacionada