0

This is a follow up question to Create array of specific counter values and Using self-defined fields in NewEnviron.

I want to use the function mtadd that fills the array mtarr depending on the value of lines, inside of the environement myenv that creates the line between the columns. To do so, I have to include the body definded outside. But this produces the error ! Misplaced alignment tab character &., apparently related to the redefinition of & starting at line 10, which I don't entirely understand (taken from Claudio Fiandrino in How can you create a vertical timeline?). Commenting out \BODY leads to the correct line, but the function mtadddoesn't work and thus myarr stays empty.

Here is the code:

\documentclass{article}
\usepackage{pgfmath,pgffor}
\usepackage{environ}
\usepackage{tikz}
\usetikzlibrary{calc,matrix}
\usepackage{charter}

\makeatletter
\let\matamp=&
\catcode`\&=13
\makeatletter
\def&{\iftikz@is@matrix
  \pgfmatrixnextcell
  \else
  \matamp
  \fi}
\makeatother

\newcounter{lines}
\def\endlr{\stepcounter{lines}\\}

\newcounter{envcount}
\setcounter{envcount}{0}

\newcommand*\myarr{}
\newcommand{\mtadd}{
\ifx\myarr\empty
\edef\myarr{\arabic{lines}}
\else\edef\myarr{\myarr,\arabic{lines}}
\fi}

\tikzset{description/.style={
  column 2/.append style={#1}
 },
 line color/.store in=\lcolor,
 line color=white!80!black,
 line color st/.style={fill=\lcolor,draw=\lcolor},
}

\NewEnviron{myenv}[1][]{%
\def\myarr{}
\setcounter{lines}{1}
\stepcounter{envcount}%
\BODY
\begin{tikzpicture}[column 1/.style={anchor=east},
 column 2/.style={anchor=west},
 #1
]
\matrix(myenv\theenvcount)[matrix of nodes]{\BODY};
\pgfmathtruncatemacro\endmtx{\thelines-1}
\path [line color st]
($(myenv\theenvcount-1-1.north east)!0.5!(myenv\theenvcount-1-2.north west)$)--
($(myenv\theenvcount-\endmtx-1.south east)!0.5!(myenv\theenvcount-\endmtx-2.south west)$);
\end{tikzpicture}
}

\def\names{{"Katie","Frank","Laura","Joe","Peter","Jens","Carolin","Anna","Steph","Stef"}}
\begin{document}
\begin{myenv}
test &  now\mtadd\endlr
test &  \foreach \i in \myarr {%
     Name \i: \pgfmathparse{\names[\i]}\pgfmathresult, }\endlr
\end{myenv}

\end{document}
PeMa
  • 175
  • 7
  • What exactly do you mean? – PeMa Feb 15 '16 at 16:08
  • It doesn't change anything if I put the \BODY in the figure or above, just if I place it before the definitions of the counters, they don't apply. Sorry, if I don't understand entirely what you mean. I'm completely new to laTex coding. – PeMa Feb 15 '16 at 16:32
  • Let's start over. As I understand it, you want to count the number of names and fill an array with (1,2,3,...} to match. – John Kormylo Feb 15 '16 at 17:23
  • What I want is the function \mtadd to fill the array \myarr only with specific line numbers of my choice. The array \myarr is supposed to be used in the figure to create a set of points. What's behind, is a generalization of the timeline in the post of Claudio Fiandrino in http://tex.stackexchange.com/questions/196794/how-can-you-create-a-vertical-timeline. I want to have different spacings between the entries of one timeline, sometimes 3 lines, somtimes 1 ... . – PeMa Feb 15 '16 at 22:15
  • Thank you. Actually the way I did it already works, if I create the array by myself, just the more elegant way of using a function like I'm trying here doesn't work. ... due to this strange error. – PeMa Feb 16 '16 at 10:34
  • Anyhow, I will think about using the measure of te box instead, even though I have doubts at the moment that this could be inculded into the method I've chosen so far. About other methods, I don't know any alternative that would create such a nice looking timeline as this one did. – PeMa Feb 16 '16 at 10:37
  • The error isn't strange. Expanding \BODY includes the & character, which causes an error if the parser isn't expecting it. – John Kormylo Feb 16 '16 at 16:21
  • So is there a way to prevent this and still get it running? I mean, I just need the function \mtadd to work inside the environment. – PeMa Feb 18 '16 at 10:14

0 Answers0