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 Jul 13 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. – RedDotRabbit Jul 13 at 19:45
  • What kind of draws will you do? Can you show us typical examples? – projetmbc Jul 13 at 19:58
  • 1
    Just added an image – RedDotRabbit Jul 13 at 20:12
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 – RedDotRabbit Jul 13 at 20:12
  • ok i guess that was a stupid question since you provided an image – RedDotRabbit Jul 13 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? – David Carlisle Jul 13 at 20:35
  • @RedDotRabbit I added a version tested with texlive 2017. – David Carlisle Jul 13 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 – RedDotRabbit Jul 14 at 7:25

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.