3

Sometimes I exploit WolframAlpha to do some simple calculations and then insert results in my latex code. But I find annoying to adapt the output given by WA to the LaTeX syntax.

When the output is a matrix the copyable (plain text) output is something like that

    a | b | c
    d | e | f
    g | h | i 

so I decided to change the catcode of the vertical bar | to that of an aligning character with the command

\catcode`|=4

of course I have to do that inside a group in order to avoid serious issues. Here I got a code that works fine, but I'm asking if it is the right way to do it and why it does not work when some modifications are added. Here is my code.

\[ \catcode`|=4 %changing the catcode to that of alignment character 
M(f_{V})=\begin{pmatrix}
    2 | 0 | 0\\
    1 | 1 | 3\\
    0 | 0 | -2
\end{pmatrix}
\] 

Putting inside a \begigroup \endgroup works fine:

\[
M=\begingroup \catcode`|=4\begin{pmatrix}
    a|b|c\\ d|e|f\\ g|h|i
\end{pmatrix} \endgroup=M(f|V) %It works either
\]

but inside `\begin{matrix}` `\end{matrix}` it does not:

\[
M= \begin{pmatrix}\catcode`|=4  %It doesn't work
    a|b|c\\ d|e|f\\ g|h|i
\end{pmatrix} 
\]

Finally, is there any difference between

\[M=\begingroup \catcode`|=4\begin{pmatrix}
    a|b|c\\ d|e|f\\ g|h|i
\end{pmatrix} \endgroup=M(f|V)
\]

and

\[{ \catcode`|=4\begin{pmatrix}
a|b|c\\ d|e|f\\ g|h|i
\end{pmatrix} }=M(f|V) \] ?

I use \documentclass{article} \usepackage{amsmath} in the preamble.

2 Answers 2

2

When it comes to aligned environments, any change you make to accommodate the | change will have to occur outside of the environment. Rather than resort to catcode changes, here I use a token cycle to literally examine the tokens in advance of execution, changing all occurrences of | to & prior to environment execution. Using an escape character (here redefined to @), I can insert actual | tokens within such an environment, as well, as shown in the 3rd example.

\documentclass{article}
\usepackage{mathtools,tokcycle}
\settcEscapechar{@}% OTHERWISE | HAS SPECIAL MEANING TO tokcycle
\Characterdirective{\ifx|#1\addcytoks{&}\else\addcytoks{#1}\fi}
\begin{document}
\tokencyclexpress
\[
M(f_{V})=\begin{pmatrix}
    2 | 0 | 0\\
    1 | 1 | 3\\
    0 | 0 | -2
\end{pmatrix}
\] 
\endtokencyclexpress
Example of aligned output
\tokencyclexpress
\begin{alignat*}{3}
|k_1     ||\geq 1 \quad\land \quad ||k_1     \leq a_1      \\
|k_2     ||\geq 1 \quad\land \quad ||k_2     \leq a_2      \\
|\qquad \cdots \\
|k_{n-1} ||\geq 1 \quad\land ||k_{n-1} \leq a_{n-1}  \\
|k_n     ||\geq 1 \quad\land ||k_n     \leq a_n      \quad \}
\end{alignat*}
\endtokencyclexpress
Escaping a vertical bar within such an environment:
\tokencyclexpress
\[
M(f_{V})=\begin{pmatrix}
    2 | 0 | 0\\
    1 | @|x|@ | 3\\
    0 | 0 | -2
\end{pmatrix}
\] 
\endtokencyclexpress
\end{document}

enter image description here

3

You can't change the category code after \begin{pmatrix}, because alignment cells form groups, so the assignment is forgotten as soon as the first | is found.

There is no significant difference between

{\catcode`|=4 \begin{pmatrix}...\end{pmatrix}}

and

\begingroup\catcode`|=4 \begin{pmatrix}...\end{pmatrix}\endgroup

so you can use either. But I have bad news for you: you can't use such matrices, even changing the category code of | inside align or other amsmath display environments, because they read the entire environment as a macro argument, which freezes the category codes.

You'd need to change the category code outside such environments, but of course this disallows using | in its standard meaning in the same environment.

I'd define a Wmatrix environment, instead. You need to add \\ manually, of course.

\documentclass{article}
\usepackage{amsmath}

\ExplSyntaxOn

\NewDocumentEnvironment{Wmatrix}{b}
 {
  \antonio_wmatrix:n { #1 }
 }
 {}

\tl_new:N \l__antonio_wmatrix_body_tl

\cs_new_protected:Nn \antonio_wmatrix:n
 {
  \tl_set:Nn \l__antonio_wmatrix_body_tl { #1 }
  \tl_replace_all:Nnn \l__antonio_wmatrix_body_tl { | } { & }
  \begin{pmatrix} \tl_use:N \l__antonio_wmatrix_body_tl \end{pmatrix}
 }

\ExplSyntaxOff

\begin{document}

\begin{align*}
M(f_{V})&=
\begin{Wmatrix}
    2 | 0 | 0\\
    1 | 1 | 3\\
    0 | 0 | -2
\end{Wmatrix}
\\
&=
\begin{Wmatrix}
    2 | 0 | 0\\
    1 | 1 | 3\\
    0 | 0 | -2\\
\end{Wmatrix}
\end{align*}

\end{document}

enter image description here

In the example I show that the environment also works inside align and that a trailing \\ is treated correctly.

2
  • Dear @egreg, adding \\ manually it is not such a problem for me. But I'm wondering if it is possible to change the carriage return catcode (or something like that) in order to get carriage return substituted by the linebreak \\.
    – Antonio
    Apr 2 at 17:39
  • @Antonio It's the same problem, but a little worse.
    – egreg
    Apr 2 at 17:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .