2

I am trying to write a new environment that takes a matrix and draws it as a matrix of nodes in tikz. This is a minimal example:

\documentclass[a4aper,pagesize]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{matrix}

\newenvironment{diagram}{
    \begin{tikzpicture}
        \matrix (m) [matrix of math nodes]
}{
\end{tikzpicture}
}

\begin{document}
    \begin{diagram}
        {
         A & B & C \\
         D & E & F \\
        };
    \end{diagram}
\end{document}

However I'd like to be able to write the diagram environment as one would write a tabular and dismiss the { ...}; used inside the environment.

I have tried using a \begingroup ... \endgroup;, but i think tikz really wants the curly braces and I do not know how to put those in the newenvironment definition without messing up my syntax.

I guess another alternative would be to use a \newcommand, but then I will have to use an ampersand replacement character, which I do not want either.

Here is a typical picture that should be generated:enter image description here

4
  • 2
    Why do you want to do that?
    – projetmbc
    Commented Jul 13, 2021 at 19:32
  • There will be some other drawn elements depending on the size of the matrix. The entire diagram will appear with different sizes quite often. I am aware that removing the {...}; is not necessary, but i think it is pretty ugly. Commented Jul 13, 2021 at 19:45
  • What kind of draws will you do? Can you show us typical examples?
    – projetmbc
    Commented Jul 13, 2021 at 19:58
  • 1
    Just added an image Commented Jul 13, 2021 at 20:12

1 Answer 1

3

You can grab the environment body but arrange & has the right catcode.

enter image description here

\documentclass[a4aper,pagesize]{scrartcl}
%\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{matrix}

\NewDocumentEnvironment{diagram}{b}{%
    \begin{tikzpicture}%
     \matrix (m) [matrix of math nodes]{#1};%
     \end{tikzpicture}%
}{}

\AddToHook{env/diagram/begin}{\catcode`\&\active}
\begin{document}
    \begin{diagram}
         AA & B & C \\
         D & E & F \\
    \end{diagram}
\end{document}

The above uses a current LaTeX, if you have an old LaTeX similar functionality is available in packages. This is tested with an Overleaf texlive 2017 image

\documentclass[a4aper,pagesize]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}

\usetikzlibrary{matrix}

\usepackage{environ}
\usepackage{etoolbox}

\NewEnviron{diagram}{%
    \begin{tikzpicture}%
     \matrix (m) [matrix of math nodes]{\BODY};%
     \end{tikzpicture}%
}

\AtBeginEnvironment{diagram}{\catcode`\&\active}
\begin{document}
    \begin{diagram}
         AA & B & C \\
         D & E & F \\
    \end{diagram}
\end{document}
7
  • have you tried your answer? Sadly, I can not get it to work Commented Jul 13, 2021 at 20:12
  • ok i guess that was a stupid question since you provided an image Commented Jul 13, 2021 at 20:23
  • @RedDotRabbit I should have said that it needs a relatively new latex, although packages are available for the most part to define similar functionality if you have an old latex. What is the banner date given when your latex runs? Commented Jul 13, 2021 at 20:35
  • @RedDotRabbit I added a version tested with texlive 2017. Commented Jul 13, 2021 at 21:11
  • I have been using latexmk 2019, version: 4.67 which internally uses pdfTeX, Version 3.14159265-2.6-1.40.20 (TeX Live 2019/Debian). that should work since you used the 2017 version Commented Jul 14, 2021 at 7:25

You must log in to answer this question.

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