GeneralIncluding a external file within "\foreach"

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
Dr.Wackelzahn
Posts: 7
Joined: Sun Oct 26, 2008 9:53 am

Including a external file within "\foreach"

Post by Dr.Wackelzahn »

Hi,
is it possible to include an external file within a \foreach function?

Code: Select all

\begin{tikzpicture}
  \foreach \x/\y/\z/\beschriftung in {1/2/0.3/Test,
                                      2/3/1/test}
    {
     \draw[fill=tab1] (\x cm,0cm) rectangle (\x cm +1 cm,\y cm);
    };
\end{tikzpicture}
I'd like to put the Data lines "1/2/0.3/Test,2/3/1/test,......" in an external file.

Is this possible and how?
I've already tried \include or \input but always ended up in an error.

Thanks for any help!
Alex

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Including a external file within "\foreach"

Post by phi »

The following is a hack … perhaps someone has a better idea:

Code: Select all

\usepackage{etoolbox}
\let\foreachfile\foreach
\makeatletter
\patchcmd{\foreachfile}%
  {\def\pgffor@values{#2, \pgffor@stop,}}%
  {\toks@\everyeof\everyeof{\noexpand}\edef\pgffor@values{\@@input#2 , \noexpand\pgffor@stop,}\everyeof\toks@}%
  {}%
  {\PackageError{xyz}{Could not patch command}{Something happened}}
\makeatother
% ...
\foreachfile \x/\y/\z/\beschriftung in {DataFile.dat}
This needs the etoolbox package.
Dr.Wackelzahn
Posts: 7
Joined: Sun Oct 26, 2008 9:53 am

Re: Including a external file within "\foreach"

Post by Dr.Wackelzahn »

Thanks!
I'll give this workaround a try.
Post Reply