1

I've a large table (about 300 entries). Sometimes, I've a sequence of entries with the same value (for a given column). In such a case, I need to omit the value. All my values are stored in variables used elsewhere, and thus, I cannot simply get rid of them. To overcome that, I created a temporary command to maintain my value. Testing my solution in plain text gives me the desired result. However, when I apply my solution in a table, the results are wrong.

MWE (I put a dot to better see where the omited values are):

\documentclass{article}
\usepackage{ifthen}

\begin{document}

\newcommand{\myval}{}

\newcommand{\setMyVal}[1]{\renewcommand{\myval}{#1}}

\newcommand{\printOnlyFirstOccurence}[1]{
    \ifthenelse{\equal{\myval}{#1}}
        {.}
        {\setMyVal{#1}\myval}%else
}

Outside table:

\printOnlyFirstOccurence{a}

\printOnlyFirstOccurence{a}

\printOnlyFirstOccurence{b}

\printOnlyFirstOccurence{b}

\printOnlyFirstOccurence{b}



Behaviour inside table:

\begin{table}[h!]
\centering
\begin{tabular}{c|c} \hline
    \printOnlyFirstOccurence{a} & a \\ \hline
    \printOnlyFirstOccurence{a} & a \\ \hline
    \printOnlyFirstOccurence{b} & b \\ \hline
    \printOnlyFirstOccurence{b} & b \\ \hline
    \printOnlyFirstOccurence{b} & b \\ \hline
\end{tabular}

\end{table}

\end{document}

In the above example, outside the table I got:

a . b . . .

Inside the table I got:

a a . . .

Can someone explain this behavior?

  • 1
    Alignment cells form groups, so the redefinition of \myval is forgotten as soon as you go to another cell. Use \gdef\myval{.} instead of \renewcommand{\myval}{#1} in the definition of \setMyVal – egreg Feb 13 '16 at 0:27
  • Thank you @egreg, your comment helped to solve my MWE. I can update my question to include a more realistic use case, where I get another behaviour, now when using the biocon package. I'm new using stackexchange, thus, if it is more common, I can accept your answer (if you post it) and create another question, instead of updating this one. – Isma Feb 13 '16 at 1:25
  • 1
    could you please post your edit as a separate question ? It introduces new-elements (non expandable \plant command) which changes the context. – user4686 Feb 13 '16 at 8:25
  • @jfbu, done in: tex.stackexchange.com/questions/292751/… – Isma Feb 13 '16 at 16:56
  • @egreg Could you post your comment as an aswer? Thanks – Isma Feb 13 '16 at 16:57
1

Alignment cells form groups, so the redefinition of \myval is forgotten as soon as you go to another cell. Use \gdef\myval{.} instead of \renewcommand{\myval}{#1} in the definition of \setMyVal. With \renewcommand you only get a local redefinition.

\documentclass{article}
\usepackage{ifthen}

\begin{document}

\newcommand{\myval}{}

\newcommand{\setMyVal}[1]{\gdef\myval{#1}}

\newcommand{\printOnlyFirstOccurence}[1]{
    \ifthenelse{\equal{\myval}{#1}}
        {.}
        {\setMyVal{#1}\myval}%else
}

Outside table:

\printOnlyFirstOccurence{a}

\printOnlyFirstOccurence{a}

\printOnlyFirstOccurence{b}

\printOnlyFirstOccurence{b}

\printOnlyFirstOccurence{b}



Behaviour inside table:

\begin{table}[h!]
\centering
\begin{tabular}{c|c} \hline
    \printOnlyFirstOccurence{a} & a \\ \hline
    \printOnlyFirstOccurence{a} & a \\ \hline
    \printOnlyFirstOccurence{b} & b \\ \hline
    \printOnlyFirstOccurence{b} & b \\ \hline
    \printOnlyFirstOccurence{b} & b \\ \hline
\end{tabular}

\end{table}

\end{document}

enter image description here

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.