Math & ScienceArrays and Expansion

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
kazza
Posts: 3
Joined: Wed Apr 25, 2012 7:07 pm

Arrays and Expansion

Post by kazza »

Recently I have found myself using arrays with quite fancy format specifications (particually for case distinction of mixfix operators). For example

Code: Select all

\begin{array}{@{\llbracket}l@{,}l@{\models}l@{\rrbracket~=~}l}
M & s & true & true \\
...
\end{array}
Then the inevitable occoured, I wanted to tweak (bold) one of the lines of the array. So I defined the following

Code: Select all

\newcommand{\ifother}[2]{{{{\ifx\myother\undefined{#2}\else{#1}\fi}}}}
\newcommand{\startother}{\def\myother{}}
\makeatletter
\newcommand{\finishother}{\let\myother\@undefined}
\makeatother
with the intention to use it in the format specification for the array to beable to switch between one symbol and another. I.e.

Code: Select all

\begin{array}{@{\ifother{\llbracket}{\boldsymbol{\llbracket}}}l@{,}...}
... \\
\startother ... \\
... \\
\finishother ... \\
... \\
\end{array}
But I soon found that the array expands its arguments early, so the ifx test occours once at the begining of the array and its expanded value is reused.

As a concrete example

Code: Select all

\begin{array}{@{\ifother{b}{a}~}l}
1 \startother\\
2 \\
3 \endother \\
4 \\
\end{array}
outputs:
a 1
a 2
a 3
a 4
but i would like it to output:
a 1
b 2
b 3
a 4
Ive done some experiments with noexpand/expandafter/aftergroup but could not find the right combination. Could anyone help?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

kazza
Posts: 3
Joined: Wed Apr 25, 2012 7:07 pm

Arrays and Expansion

Post by kazza »

ah, I found my own solution. I had to use the latex protect command, and use global defs. i.e.

Code: Select all

    \newcommand{\ifother}[2]{{{{\ifx\myother\undefined{#2}\else{#1}\fi}}}}
    \newcommand{\startother}{\global\def\myother{}}
    \makeatletter
    \newcommand{\finishother}{\global\let\myother\@undefined}
    \makeatother
and

Code: Select all

\begin{array}{@{\protect\ifother{b}{a}~}l}
Post Reply