I'd like to make a macro that expands to the contents of a file. Basically something like
\def{\mycommand}{\input{file.txt}} % file.txt contains "Hello%"
\anothercommand{\mycommand}
should be completely equivalent to:
\def{\theircommand}{Hello}
\anothercommand{\theircommand}
The file shall be input
ted when the macro is used, not when it is defined (since I'm generating it on the fly and it may change throughout the document). The reason I'm doing this is because I want to emulate \pdf@filemoddate
, which I need, but which is not available in the old pdfLaTeX version I have at work. This is a well, almost minimal, non-working example with further information:
\documentclass{article}
\makeatletter
% This part is from epstopdf. I can't change this.
% It just takes a string like D:20090114124316Z and
% formats it as a date.
% --------------------------------------------------
\def\ETE@Date#1\@nil{%
\ifx\\#1\\%
\else
\ETE@@Date#1\@nil
\fi
}%
\def\ETE@@Date#1:#2#3#4#5#6#7#8#9{%
date: #2#3#4#5-#6#7-#8#9 %
\ETE@@Time
}%
\def\ETE@@Time#1#2#3#4#5#6#7\@nil{%
#1#2:#3#4:#5#6%
}%
\makeatother
% --------------------------------------------------
% some commands of increasing complexity
% this one just expands to the string I want out:
\def\blahone{D:20090114124316Z}
% this is the core functionality that I'm missing:
\def\blahthree{\input{temp.txt}}
% It seems to be not a problem with 'input', but an
% expansion problem, as this doesn't work either:
\def\blahtwo{\blahone}
% This is very close to the actual command I want to define
\def\myfiledate#1{%
% in reality I call a little script with #1 here
\immediate\write18{cat temp.txt > temp2.txt}%
\input{temp2.txt}%
\immediate\write18{rm temp2.txt}%
}
\begin{document}
\makeatletter
All macros work on their own. There must not be spaces around the Xs: \\
X{\blahone}X \\
X{\blahtwo}X \\
X{\blahthree}X \\
X{\myfiledate{test.tex}}X \\
However, I can't use the results of the more complex ones as arguments: \\
\expandafter\ETE@Date\blahone\@nil \\
% doesn't work:
%\expandafter\ETE@Date\blahtwo\@nil \\
% need the 'romannumeral' trick to force full expansion:
\expandafter\ETE@Date\romannumeral-`X\blahtwo\@nil \\
% but that isn't an option. I have code that calls
% \expandafter\ETE@Data\MACRO\@nil
% and I can only replace \MACRO, not change that code!
% doesn't work at all, no matter how many expandafters I try:
%\expandafter\ETE@Date\blahthree\@nil \\
%\expandafter\ETE@Date\filedate\@nil \\
\makeatother
\end{document}
The file test.txt contains only
D:20090114124316Z%
(with no newline at the end).
Update (what I'm trying to achive): I'm have a large document with lots of figures (eps) in it. When compiling it with pdfLaTeX, epstopdf converts the eps files only as needed to pdf. However, on my PC at work, the pdffilemoddate command is missing, and epstopdf can't tell if an eps file has been modified or not, so it insists on converting all figures again each time, which takes incredibly long. I'd like to have a \def\pdf@filemoddate#1{...}
replacement that I can place in my header, and that permits epstopdf to function as intended.
catchfile
package,catchfile
is nice. However, I'd need to somehow pass a parameter to thesetup
code of CatchFileDef, to create a macro that takes an argument.\readdef
command of the readarray package does this. I'll look further at your MWE to see if it fits.catchfile
won't work. It reads the file when I call\CatchFileDef{\macro}{file.txt}{}
. If file.txt changes between invocations of\macro
, it will have no effect.