I am posting this as a general question prior to introducing the specific use case. Apologies if this has already been posted and I am using the incorrect terms for what I want, my searches have been relatively fruitless.
General Case
I want to create a generic environment template that is passed commands from inside the environment. These commands then define how the environment renders on compile. I want to do this so the number of optional commands I pass to the environment is arbitrary for formatting purposes. Additionally, I want to add a large number of optional commands while avoiding a giant string of {}{}{}{}
.
Here is a non-working example of what I want:
\documentclass{article}
\newenvironment{testenv}%
{%
This is \Alice\Bob
\begin{center}
I want to talk about \Bob
}%
{%
\end{center}
}%
\newcommand{\Alice}[1]{#1}
\newcommand{\Bob}[1]{#1}
\begin{document}
\begin{testenv}
\Alice{asdf}
\Bob{jkl}
this is everything
\end{testenv}
\begin{testenv}
\Bob{jkl}
this is just Bob
\end{testenv}
\end{document}
In this example, the custom environment always prints "I want to talk about \Bob" along with additional text. However, the header for the verbatim
environment is dependent on what, if anything, we put in \Alice
and \Bob
.
Relevant Posts
- I know how to make arguments optional using the
ifthen
package as described in this question: https://stackoverflow.com/questions/1812214/latex-optional-arguments. I did not include that part in this example for simplicity purposes. - I know that optional arguments are possible for commands, but I don't know how this works with environments: Optional arguments in "families"... When and how?.
The Ask
- Obviously this doesn't work. Am I completely off base when thinking about the relationship between commands and environments in TeX?
- Is there a way to make this general example work?
- Is this a case for
xparse
? I have never used it, so how would I do this?
Specific Case
What I really want to do is create a custom recipe environment with relevant information in the margins (this is a niche case, so I figured I would pose the general question first). For this, I am using the marginnote
package. There is a lot of information I want to have available to the margin, and I want to do it with variables for ease of replication. Currently, I have individual commands for different elements I want in the box. From my style template (This is not a MWE):
\newcommand{\serves}[1]{%
\marginnote{Serves: #1~\\*}%
}
\newcommand{\vegetarian}{%
\marginnote{\textcolor{forestgreen}{\textbf{Veggie}}}~\\*%
}
\newcommand{\vegan}{%
\marginnote{\colorbox{green!20}{\textbf{Vegan}}}~\\*%
}
\newcommand{\preptime}[1]{%
\marginnote{Prep: #1}~\\*%
}
\newcommand{\cooktime}[1]{%
\marginnote{Cook time: #1}~\\*%
}
Individual recipes require specific combinations of these, often multiple. You will notice the hacky ~\\*
at the end of each command. If I do not include this, the boxes overlap each other. If I do include it, the line breaks also persist in the document. See example image:
I am converting my hacky solution to a cleaner template that avoids the overlap problem in my ``newenvironment{recipe}, but I am yet to find a way to do this programatically in the margins with all of the optional arguments that I want.
The Ask
- If there is a specific tool I should use to achieve the goals for my specific case, please feel free to comment on it and/or include it in an answer to the general case. However, please prefer answering the general question.
~\\
is a very weird construct, probably just use\par