# erraneous text in a LaTeX tabular array created using custom macros

I have a tabular array that is based on David Carlisle's answer to Generating LaTeX tabular code during compilation. I have modified the code so that columns and rows are defined in different \newcommand command definitions (practically lists). \regionlist contains brain regions {ABC,DEF,GHI} and \contrastlist contains 'contrasts' or activities {SLEEP,RUN,SPEAK}. These are defined in g_loc_contrast_tabular_def.tex. The data for the cells is defined in g_loc_contrast_def.tex using \def macro definitions, one for each data cell of the tabular array, excluding the top row and the left-most column.

So this particular array contains data on localized neural activity in different situations, the situations are on rows, brain regions on columns, and the data are defined by macros such as \def\locBABCcontrastSLEEP{both ABCs during sleep}. These definitions work fine.

I have added code to replace row headers with their descriptions that are defined in g_loc_contrast_def.tex using \def macros (\contrastdescSLEEP, \contrastdescRUN, \contrastdescSPEAK), but I have not been able to get the visual output right. On the leftmost column, I want "deep sleep" on the 1st data row (2nd row), "running marathon" on the 2nd data row (3rd row), and "speaking" (all with bold, without quotes), but I get LEEP deep sleep SSLEEP", "UN running marathon RRUN" and "PEAK speaking SSPEAK" (without quotes). The strings RUN, SLEEP and SPEAK are used in macro names, and when I finally found out a way to get the description in the array by using \textbf{\newcommand\contrastdesc#1{\csname contrastdesc#1\endcsname} \contrastdesc#1}%, the first letter of the #1 string, that holds the row-code (SLEEP, RUN or SPEAK), was moved to the right side of the description string together with complete row-code string. So I'm trying to find out what is wrong with my LaTeX code to get the output right.

I'm total beginner in LaTeX macros, so it might be just some basic mistake. I have tried to find a solution and to understand LaTeX macro system, but I think I understand still only a tiny bit of it.

The code compiles without problems by using latexmk --pdf main.tex.

\documentclass[12pt]{article}

\usepackage[american]{babel}
\usepackage{graphicx}

\begin{filecontents}{g_loc_contrast_tabular_def}
\newcommand\regionlist{ABC,DEF,GHI}
\newcommand\contrastlist{SLEEP,RUN,SPEAK}

% listheadings.
\def\listheadings{%
\expandafter\xlistheadings\regionlist,\relax,}

% xlistheadings.
\def\xlistheadings#1,{%
\ifx\relax#1%
\expandafter\\\hline
\else
&\textbf{#1}%
\expandafter\xlistheadings
\fi}

% listbody.
\def\listbody{%
\expandafter\xlistbody\contrastlist,\relax,}

% xlistbody.
\def\xlistbody#1,{%
\ifx\relax#1%
\else
\textbf{\newcommand\contrastdesc#1{\csname contrastdesc#1\endcsname}
\contrastdesc#1}%
\gdef\thisrow{#1}
\expandafter\xlistdata\regionlist,\relax,%
\expandafter\hline
\expandafter\xlistbody
\fi}

% xlistdata.
\def\xlistdata#1,{%
\ifx\relax#1%
\expandafter\\
\else
&\csname locB#1contrast\thisrow\endcsname
\expandafter\xlistdata
\fi}

% preamble.
\def\preamble{\expandafter\xpreamble\regionlist,\relax,}
\def\xpreamble#1,{%
\ifx\relax#1%
\else
c%
\expandafter\xpreamble
\fi}

% tablestart.
\def\tablestart{%
\edef\temp{\noexpand\begin{tabular}{c\preamble}\noexpand\hline}%
\temp}
\end{filecontents}

\begin{filecontents}{g_loc_contrast_def}
\def\locBABCcontrastSLEEP{ both ABCs during sleeping }
\def\locBABCcontrastRUN{ both ABCs during running }
\def\locBABCcontrastSPEAK{ both ABCs during speaking }

\def\locBDEFcontrastSLEEP{ both DEFs during sleeping }
\def\locBDEFcontrastRUN{ both DEFs during running }
\def\locBDEFcontrastSPEAK{ both DEFs during speaking }

\def\locBGHIcontrastSLEEP{ both GHIs during sleeping }
\def\locBGHIcontrastRUN{ both GHIs during running }
\def\locBGHIcontrastSPEAK{ both GHIs during speaking }

\def\contrastdescSLEEP{ deep sleep }
\def\contrastdescRUN{ running marathon }
\def\contrastdescSPEAK{ speaking }
\end{filecontents}

\begin{filecontents}{g_loc_contrast_tabular}
\tiny
\begin{table}
\scalebox{0.7}{
\tablestart
\listheadings
\listbody
\end{tabular}
} % end scalebox
\caption{Caption goes here.}
\begin{enumerate}
\item Item number one.
\item Item number two.
\end{enumerate}
\end{table}
\normalsize
\end{filecontents}

\input{g_loc_contrast_tabular_def}
\input{g_loc_contrast_def}

\begin{document}

\input{g_loc_contrast_tabular}

\end{document}

-

## 1 Answer

I think you just want

\def\xlistbody#1,{%
\ifx\relax#1%
\else
\textbf{\csname contrastdesc#1\endcsname}%
\gdef\thisrow{#1}
\expandafter\xlistdata\regionlist,\relax,%
\expandafter\hline
\expandafter\xlistbody
\fi}


-
Yes, that's correct. Thanks a lot. –  nrz Apr 22 '12 at 19:45