0

I would like to populate a table, which I created in my LaTeX document, using iterating over cells of a text file.

In order to read the text file I use this code.

\newread\data
\openin\data = data
\loop\unless\ifeof\data
\read\data to\cell
% do stuff with \cell
\repeat
\closein\file

As you know, \read reads until it encounters a line break. Nevertheless, I would not like this behavior and I am more interested in reading cell by cell. In particular, reading by a certain delimiter. For example, tab, ;, or whatever else.

TL;DR: Is there a command similar to \read to keep reading until it reaches to a certain delimiter?

Note: I already know there is a CSV package out there which just do what I am trying to do. However, in this case I would rather prefer reinventing the wheel!


UPDATE #1:

Example of the text file:

1   2   3   4
A   B   C   D
  • An example of the file to be read in? – egreg Oct 31 '15 at 20:51
  • It depends. You either read line by line (to something, and then parse each line for ; or whatever) or use \input rather than \read in which case you can just parse in the normal way. – David Carlisle Oct 31 '15 at 20:52
  • @egreg Example has been added. tab is the delimiter. – Hi I'm Frogatto Oct 31 '15 at 20:53
  • @abforce TeX will read line by line anyway. You can split the line after it has been read and do a loop on the items. – egreg Oct 31 '15 at 20:55
  • @DavidCarlisle I'm new in LaTeX. Could you please explain how can I parse each line? – Hi I'm Frogatto Oct 31 '15 at 20:55
1

enter image description here

\documentclass{article}

\makeatletter
\def\zz#1 {\def\zza{#1}%
\ifx\zza\zzb\else
\stepcounter{enumi}%
Field \theenumi\ is \fbox{\zza}\par
\expandafter\expandafter\expandafter\zz
\fi}
\def\zzb{\relax?}

\begin{document}

\expandafter\zz\@@input testdata.txt\relax?


\end{document}
  • Thanks very much sir. It seems your codes are very different with mine. I couldn't understand, though. Thanks again anyway. – Hi I'm Frogatto Oct 31 '15 at 21:06
  • @abforce you asked for low level code, not a packaged interface, so this is probably as simple/short as it can be. As I say there are several answers describing delimited macro arguments. – David Carlisle Oct 31 '15 at 21:08
  • Excuse me sir. Isn't there a way to just split my \cell variable in a in-line way? – Hi I'm Frogatto Oct 31 '15 at 21:10
  • 1
    (don't call me sir:-) the above code spits up its input on spaces you could use exactly the same command applied to the expansion of \cell to split up each line separately, although that isn't really necessary if you are splitting the whole file, as you see. – David Carlisle Oct 31 '15 at 21:13
  • OK. Thank you for your time and consideration. – Hi I'm Frogatto Oct 31 '15 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.