表中的巨集擴充排序

表中的巨集擴充排序

我有一張大表(大約 300 個條目)。有時,我有一系列具有相同值的條目(對於給定列)。在這種情況下,我需要省略該值。我的所有值都儲存在其他地方使用的變數中,因此,我不能簡單地刪除它們。為了克服這個問題,我創建了一個臨時命令來維持我的價值。以純文字測試我的解決方案給了我想要的結果。但是,當我在表中應用我的解決方案時,結果是錯誤的。

MWE(我加了一個點以便更好地查看省略的值在哪裡):

\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}

在上面的例子中,在表之外我得到:

A 。 b. 。 。

在表裡面我得到:

一 。 。 。

有人可以解釋這種行為嗎?

答案1

\myval對齊單元格形成群組,因此一旦您轉到另一個單元格,就會忘記的重新定義。在 的定義中使用\gdef\myval{.}代替。有了你就只能得到局部的重新定義。\renewcommand{\myval}{#1}\setMyVal\renewcommand

\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}

在此輸入影像描述

相關內容