Cruzar datos externos

Cruzar datos externos

¿Existe algún módulo que pueda usarse para hacer referencia a datos externos? Por ejemplo, me gustaría agregar el resultado de mi análisis a algún tipo de biblioteca y darle una clave de cita que pueda usar en todo mi texto. La idea es evitar errores/problemas de constancia mediante el uso de una clave de citación y poder actualizar rápidamente los datos a lo largo de mi manuscrito si se modifica el análisis.

Respuesta1

Proporcionaría un archivo externo, por ejemplo, myData.txtcon el siguiente contenido:

% Content of myData.txt
\newcommand{\myVariablePi}{3.14} 
\newcommand{\myVariableEuler}{2.71} 

En su documento principal, puede importar este archivo con \input{myData.txt}. Luego puede usarlo \myVariablePicomo variable en el documento.

Nota al margen 1:Es posible que necesite un \myVariablePi{}( {}al final) si desea tener un espacio después de la variable (o usar xspaceel paquete).

Nota al margen 2:Asegúrese de que sus declaraciones sigan siendo ciertas después de actualizar las variables :).

Respuesta2

A la luz del comentario, puede hacer lo siguiente: Crear un archivo my-variables.styque contenga, por ejemplo

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{my-variables}
\RequirePackage{siunitx}

% Commands for setting and getting variables
\newcommand{\setvariable}[2]{
    \expandafter\def\csname myv@riable@#1\endcsname{#2}
}
\newcommand{\getvariable}[1]{%
    \csname myv@riable@#1\endcsname
}

% My variable variable definitions
\setvariable{speed-of-light}{\SI{299 792 458}{m/s}}

y colóquelo en el texmf-tree de su casa. Para mí en una computadora con Linux, esto es ~/texmf/tex/latex/local. El directorio correcto para usted debe encontrarse ejecutándolo kpsewhich -var-value TEXMFHOMEen la terminal.

Ahora puedes hacer uso de tu “biblioteca”, por ejemplo, escribiendo el siguiente archivo tex:

\documentclass{article}
\usepackage{my-variables}
\begin{document}
    The speed of light is \getvariable{speed-of-light}.
\end{document}

Esta debería ser la velocidad correcta de la luz guardada en su my-variables.styarchivo.

Nota al margen:Imagine que desea resaltar todas las variables que se leen del archivo de su biblioteca. Un caso de uso para esto que podría imaginar es que desea hojear rápidamente un documento y asegurarse de que todas las cantidades se lean desde su biblioteca. Con mi solución sugerida, puedes simplemente hacer

\renewcommand{\getvariable}[1]{%
    \colorbox{yellow!50}{\csname myv@riable@#1\endcsname}
}

o lo que quieras hacer para resaltar ecuaciones.

Respuesta3

Desarrollando sobre elsolución de bubaya se podría agregar un poco de gestión de errores en caso de que uno, por error, por ejemplo debido a un error tipográfico, intente obtener valores que no están definidos o intente definir valores que ya están definidos.

Pongo el código donde están las macros para configurar y recuperar valores.definidoen un archivo .sty por su cuenta cuyo nombre esEstablecerValores.sty.
Pongo el código donde están estas macrosusadopara establecer los valores en otro archivo .sty por su cuenta cuyo nombre esMisValores.sty.

Al compilar el ejemplo siguiente, estos archivos .sty se crearán automáticamente debido a los filecontents*entornos.

Si lo desea, puede fusionar los dos archivos .sty.

No lo hice porque es posible que uno desee utilizar estas macros también con otros proyectos y otros conjuntos de valores. ;-)

\documentclass{article}

% Let LaTeX create the file SetValues.sty in case it does not already exist
% on the system:
\begin{filecontents*}{SetValues.sty}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{SetValues}
\newcommand\SetValues@Exchange[2]{#2#1}%
\newcommand\SetValues@Name{}%
\long\def\SetValues@Name#1#{\romannumeral0\SetValues@@Name{#1}}%
\newcommand\SetValues@@Name[2]{%
  \expandafter\SetValues@Exchange\expandafter{\csname#2\endcsname}{ #1}%
}%
\DeclareRobustCommand\GetValue[1]{%
  \@ifundefined{SetValues@@@#1}%
               {%
                 \SetValues@UndefText
                 \GenericError{\space\@spaces\@spaces}%
                   {Error: Value `#1' not defined}%
                   {Source for further information on this error is neither available nor needed.}%
                   {It was attempted to obtain a value with name `#1'\MessageBreak%
                    although such a value is not defined.\MessageBreak%
                    Either the intended value has another name (typo or the like?)\MessageBreak%
                    or it needs to be defined.%
                   }%
               }{%
                 \SetValues@Name{SetValues@@@#1}%
               }%
}%
\DeclareRobustCommand\SetValue[1]{%
  \@ifundefined{SetValues@@@#1}%
               {\SetValues@Name\newcommand*{SetValues@@@#1}}%
               {%
                 \GenericError{\space\@spaces\@spaces}%
                   {Error: Value `#1' already defined}%
                   {Source for further information on this error is neither available nor needed.}%
                   {A value with name `#1' is already defined.\MessageBreak%
                    Either choose another name for the value\MessageBreak%
                    or modify the existing definition.%
                   }%
               }%
}%
\@onlypreamble\SetValue
\AtBeginDocument{%
  \@ifpackageloaded{hyperref}{%
    \DeclareRobustCommand\SetValues@UndefText{%
      \texorpdfstring{\nfss@text{\reset@font\bfseries ??}}{??}%
    }%
  }{%
    \DeclareRobustCommand\SetValues@UndefText{%
      \nfss@text{\reset@font\bfseries ??}%
    }%
  }%
}%
\endinput
\end{filecontents*}

% Let LaTeX create the file MyValues.sty in case it does not already exist
% on the system:
\begin{filecontents*}{MyValues.sty}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{MyValues}
\RequirePackage{SetValues}
%
% At this place you can require whatever additional packages you 
% need for having LaTeX typeset your values nicely:
\RequirePackage{siunitx}
%\RequirePackage ...
%
% Now do a sequence of calls to \SetValue for defining values:
\SetValue{ApproxPi}{\num{3.1415927}}%
%\SetValue...
%
\endinput
\end{filecontents*}

%\usepackage{SetValues} % Actually you don't need to require the 
                        % SetValues-package as it is also required
                        % by the MyValues-package.
\usepackage{MyValues}

\begin{document}

$\pi$ is approximately \GetValue{ApproxPi}

\end{document}

información relacionada