Graphics, Figures & Tablesdefining environments or commands

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Marioha
Posts: 1
Joined: Mon Dec 05, 2022 11:12 am

defining environments or commands

Post by Marioha »

Hello,
I'm starting using tikz for drawing a lot of mindmaps and I need environments or commands to simplify the task. So I
defined an environment for a mindmap like this:

Code: Select all

\newenvironment{tree}[2]{
\begin{center}
\tikz \draw (0,-10) node [anchor=east,fill=orange!20] {some title};
\tikz[mindmap,concept color=black,text=white]
\node [concept] {\small #1\\#2}
}{
  ;
\end{center}
}
and this allows me to use to add children nodes like this

Code: Select all

\begin{tree}{ABC}{XYZ}
   child[concept color=blue,grow=0] {node[concept] {Y20}}
   child[concept color=blue,grow=-180] {node[concept] {Y200}}
\end{tree}
However all my attempts at defining a command of environment to add a child failed with tikz complaining about having to give up on the path, and a possible missing semicolon. Here is what I tried out as an environment, and also as a command:

Code: Select all

\newenvironment{tchild}[3]{
  child[concept color=#2,grow=#3] {node[concept] {#1}}
}{
}
Using a tikzpicture in the tree environment did not work:

Code: Select all

\newenvironment{treep}[2]{
\begin{center}
 \begin{tikzpicture}[mindmap,concept color=black,text=white]
  \draw (0,-10) node [anchor=east,fill=orange!20] {some title};
  \node [concept] {\small #1\\#2}
}{
 \end{tikzpicture}
\end{center}

}
What am I doing wrong? Or is it the case I have to use some kind of preprocessor before running LaTeX?
Thanks in advance for any help!
This is a minimal mostly-non-working example:

Code: Select all

\documentclass[10pt]{report}

\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{mindmap}

\newenvironment{tree}[2]{

\begin{center}
\tikz \draw (0,-10) node [anchor=east,fill=orange!20] {some title};
\tikz[mindmap,concept color=black,text=white]
\node [concept] {\small #1\\#2}
}{
  ;
\end{center}

}

\newenvironment{treep}[2]{

\begin{center}
 \begin{tikzpicture}[mindmap,concept color=black,text=white]
  \draw (0,-10) node [anchor=east,fill=orange!20] {some title};
  \node [concept] {\small #1\\#2}
}{
 \end{tikzpicture}
\end{center}

}

%%%% this fails, also as command
 % name, color, position
\newenvironment{tchild}[3]{
  child[concept color=#2,grow=#3]
  {node[concept] {#1}}
}{
}

\begin{document}

This works:

\begin{verbatim}
\begin{tree}{ABC}{XYZ}
   child[concept color=blue,grow=0] {node[concept] {Y20}}
   child[concept color=blue,grow=-180] {node[concept] {Y200}}
\end{tree}
\end{verbatim}

\begin{tree}{ABC}{XYZ}
   child[concept color=blue,grow=0] {node[concept] {Y20}}
   child[concept color=blue,grow=-180] {node[concept] {Y200}}
\end{tree}

But not this:
\begin{verbatim}
\begin{treep}{ABC}{XYZ}
   child[concept color=blue,grow=0] {node[concept] {Y20}}
   child[concept color=blue,grow=-180] {node[concept] {Y200}}
\end{treep}
\end{verbatim}

nor this:
\begin{verbatim}
\begin{treep}{ABC}{XYZ}
  \begin{tchild}{Y20}{blue}{0}
    \end{tchild}
  \begin{tchild}{Y200}{}{blue}{-180}
    \end{tchild}
\end{treep}
\end{verbatim}

nor this:
\begin{verbatim}
\begin{tree}{ABC}{XYZ}
  \begin{tchild}{Y20}{blue}{0}
    \end{tchild}
  \begin{tchild}{Y200}{}{blue}{-180}
    \end{tchild}
\end{tree}
\end{verbatim}

\end{document}

Recommended reading 2024:

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

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

defining environments or commands

Post by Stefan Kottwitz »

Hi Marioh,

welcome to the forum!

Indeed, a semicolon is missing after the \node command in the treep environment. Add it, then it compiles.

Stefan
LaTeX.org admin
Post Reply