1

Hello I'm fairly new with Latex. I can't understand right now how to create a command which allows you to fill a table (previously created) with the content specified in input to the command you use. Suppose you have the two commands: \inMyTable and \createMyTable. They are the commands I would like to create.

\inMyTable{1}{Books}{10/14/16}
some other text here
\inMyTable{2}{Magazines}{10/15/16}
\inMyTable{3}{Computers}{10/16/16}
\createMyTable

Would create a table with the following content:

1 | Books     | 10/14/16
2 | Magazines | 10/15/16
3 | Computers | 10/16/16

I know how to create simple commands and how to use them. But it seems this requires something else like data structures or else I don't know. Sadly I don't know what to search on the web, I can't find any useful keyword for this problem.

Would somebody help me understand how to create a simple command to achieve this goal?

  • 1
    \inTable and \createTable are not standard (or even commonly seen) commands, which file do you have that defined them? – David Carlisle Oct 14 '16 at 15:33
  • latex is a macro processing language it doesn't really have any data structures: every command essentially is just defined to be a list of tokens which replace the command name in the input stream. – David Carlisle Oct 14 '16 at 15:35
  • @DavidCarlisle it's actually the commands I would like to create/use. That was an example. I've edited the question, thank you. – user3156806 Oct 14 '16 at 15:35
  • why do you want that syntax which is very non-idiomatic for tex rather than the standard latex tabular syntax? – David Carlisle Oct 14 '16 at 15:36
  • @DavidCarlisle maybe the question was not so clear. The table should be updated automatically depending on the content I decide to write within the article (using the command)... so it's not like creating a static table. – user3156806 Oct 14 '16 at 15:40

2 Answers2

1

enter image description here

\documentclass{article}

\newcommand\startMyTable{\def\MyTable{\begin{tabular}{|l|l|l|}}}

\newcommand\inMyTable[3]{%
 \edef\MyTable{%
   \expandafter\unexpanded\expandafter{\MyTable#1&#2&#3\\}}}

\newcommand\createMyTable{\begin{center}\MyTable\end{tabular}\end{center}}
\begin{document}



\startMyTable   


\inMyTable{1}{Books}{10/14/16}
some other text here
\inMyTable{2}{Magazines}{10/15/16}
\inMyTable{3}{Computers}{10/16/16}
\createMyTable

\end{document}
David Carlisle
  • 757,742
  • This requires the rows to be input in sequence. It is not clear (to me at least) whether that is a requirement of the OP or whether he/she would like the rows to index by the first column. – Steven B. Segletes Oct 14 '16 at 15:47
  • @StevenB.Segletes yes it appeared that the first argument was just cell data for the first column, not an index into an arbitrary line. – David Carlisle Oct 14 '16 at 15:49
  • Ok I perfectly understand. Is it possible to move this table in a certain position of the article? Where \startMyTable command appear? What would be needed in that case? – user3156806 Oct 14 '16 at 16:01
  • Thank you so much in advance, I'm going to set this answer as the solution of the problem. – user3156806 Oct 14 '16 at 16:02
  • @user3156806 start can be anywhere before you start adding rows and the table appears wherever you put \createMyTable – David Carlisle Oct 14 '16 at 16:35
  • @DavidCarlisle yea what if i would need to visualize that table at the beginning of the article, and not after \createMyTable? – user3156806 Oct 14 '16 at 19:49
  • @user3156806 then that would be a different question and would obviously require writing to an external file and having the table appear only on the second run, like a table of contents – David Carlisle Oct 14 '16 at 19:51
  • @DavidCarlisle thank you for giving me the clue, I'll look for some ideas about it! – user3156806 Oct 14 '16 at 20:04
1

Since the OP explicitly provided a row index as the first argument to \inMyTable, I took that to mean that there existed the possibility that the rows could be input out of order. Otherwise, there would be no need to pass the row index, and instead just use an incremented counter to advance rows.

With that in mind, here's a token-based version that allows the rows to be entered in any order and, at the time of row input (by way of indirect addressing), sorted by the column-1 index.

\documentclass{article}
\usepackage{ifthen}
\newcounter{rowindex}
\newtoks\mytabtoks
\newcommand{\AddToToks}[2]{#1\expandafter{\the#1#2}}
\newcommand{\XAddToToks}[2]{\expandafter\AddToToks\expandafter#1\expandafter{#2}}
\newcommand\inMyTable[3]{%
  \expandafter\def\csname itr[#1,1]\endcsname{#2}%
  \expandafter\def\csname itr[#1,2]\endcsname{#3}%
  \ifnum#1>0\csname MyTabRows\endcsname\relax\def\MyTabRows{#1}\fi%
}
\newcommand\createMyTable{%
  \setcounter{rowindex}{0}%
  \whiledo{\value{rowindex}<\MyTabRows}{%
    \ifnum\therowindex>0\relax\AddToToks\mytabtoks{\\}\fi%
    \stepcounter{rowindex}%
    \edef\colA{\therowindex&
     \csname itr[\therowindex,1]\endcsname &
    \csname itr[\therowindex,2]\endcsname}%
    \XAddToToks\mytabtoks{\colA}%
  }%
  \begin{tabular}{|r|l|r|}
  \the\mytabtoks
  \end{tabular}%
}
\newcommand\resetMyTable{%
  \setcounter{rowindex}{0}%
  \whiledo{\value{rowindex}<\MyTabRows}{%
    \stepcounter{rowindex}%
    \expandafter\let\csname itr[\therowindex,1]\endcsname\relax%
    \expandafter\let\csname itr[\therowindex,2]\endcsname\relax%
  }%
  \def\MyTabRows{0}%
  \mytabtoks{}%
}
\begin{document}
\inMyTable{4}{Computers}{10/16/16}
\inMyTable{1}{Books}{10/14/16}
some other text here
\inMyTable{2}{Magazines}{10/15/16}

\createMyTable
\bigskip

\resetMyTable
\inMyTable{3}{Magazines}{10/15/16}
some other text here
\inMyTable{1}{Books}{10/14/16}

\createMyTable
\end{document}

enter image description here

  • Thank you for this, the order was not so important actually. But this gave me a nice tip to improve the way I write my macros you again. – user3156806 Oct 14 '16 at 19:46