GeneralDefine own environment with the option to drop its content

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
stietz
Posts: 4
Joined: Sun Feb 22, 2009 4:25 am

Define own environment with the option to drop its content

Post by stietz »

The idea is that I have an enviroment like that:

Code: Select all

\begin{myenv}[line width=5pt,scale=3]{myoptions}
    \draw (-1.5,-1.5) -- (1.5,1.5);
\end{myenv}
Depending on myoptions that should either expand to:

Code: Select all

\begin{tikzpicture}[line width=5pt,scale=3]
  \draw (-1.5,-1.5) -- (1.5,1.5);
\end{tikzpicture}
or be completly replaced like:

Code: Select all

You have choosen: myoptions. Hence, the tikzpicture is not displayed.
Technicly I could use

Code: Select all

\newenvironment{\myenv}[2][]{
    	\ifthenelse{\equal{#2}{conditon}}{%
    	    	\begin{tikzpicture}[#1]
    	}{%
    	    	You have choosen: #2. Hence, the tikzpicture is not displayed.
    	}
}{%
    	\ifthenelse{\equal{#2}{conditon}}{%    	
    	    	\end{tikzpicture}
    	}{}
}
But that will actually not get rid off the content itself. So, I thought that \newenvironment creates to commands \myenv and \endmyenv, so that I can use something like:

Code: Select all

\long\def\myenv[#1]#2#3\endmyenv{
	\ifthenelse{\equal{#2}{conditon}}{%
		\begin{tikzpicture}[#1]%
		#3
		\end{tikzpicture}
	}{%
		You have choosen: #2. Hence, the tikzpicture is not displayed.
	}
}
But this produces the error:
! File ended while scanning use of \myenv.
<inserted text>
\par

Recommended reading 2024:

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

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

phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Define own environment with the option to drop its content

Post by phi »

This doesn't work since \myenv scans for \endmyenv which doesn't occur in the document (\end{myenv} is something different). A quick, but not unproblematic way to grab the contents of an environment as a macro argument is used by the amsmath environments like align. From the amsmath documentation:
The macro \collect@body starts the scan for the \end{...} command of the current environment. It takes a macro name as argument. This macro is supposed to take the whole body of the environment as its argument. For example, \begin{align} would call \collect@body\@align if @align#1{...} is the macro that sets the alignment with body #1.
benm
Posts: 2
Joined: Tue Nov 17, 2009 1:05 am

Define own environment with the option to drop its content

Post by benm »

I'm a n00b, so I didn't understand what the answer meant. After messing around for a while, I devised the following MWE.

Code: Select all

\documentclass{report}
\usepackage{amsmath}

\makeatletter
\newcommand{\mycommand}[1]{aa#1zz}
\newenvironment{myenv}{begin \collect@body\mycommand }{ end}
\makeatother

\begin{document}
\begin{myenv}qwerty\end{myenv}
\end{document}
% Result is "begin aaqwertyzz end"
benm
Posts: 2
Joined: Tue Nov 17, 2009 1:05 am

Define own environment with the option to drop its content

Post by benm »

Another note. \collect@body was designed to fail when the environment contains multiple \par(agraph)s. Luckily, there's a replacement called \Collect@Body in the environ package:

http://www.ctan.org/tex-archive/macros/ ... b/environ/
Post Reply