# Using commands inside environment definitions

I want to do something like

\newenvironment{definition}
{
\textit{
}
{
}
}


but of course it doesn't work, the braces get mixed up. (In other words, I want that within my environment, the content is surrounded by a command such as \textit or \textbf for instance.) Any ideas?

EDIT : My environment now looks like this :

\newenvironment{definition}
{
\textbf{\underline{Definition.}}
\vspace{12 pt}
\begin{itshape}
}
{
\end{itshape}
}

-
–  hpesoj626 Mar 9 '13 at 2:08

In your given example, you can define

\newenvironment{myenv}{\itshape}{}


Actually, the way environments work, you could just use

\begin{itshape} <content> \end{itshape}


but don’t, it is better to have environments named in a meaningful semantic way, say

\newenvironment{emphasize}{\itshape}{}
\begin{emphasize} <content> \end{emphasize}


If you really need to use a macro that expects a parameter (like \textit) you can use the environ package and its \BODY macro as demonstrated by Werner.

-
Thanks! Actually I chose the \begin{itshape}/\end{itshape} first and it work just fine... what exactly is wrong with it? –  Patrick Da Silva Mar 9 '13 at 3:08
@PatrickDaSilva it is better to have a meaningful name for the environment. It's purpose surely isn't a demonstration of italic font shape, is it? IMHO the name of the environment should reflect its meaning and not what it is actually doing. –  clemens Mar 9 '13 at 8:25
I edited my question to show what I actually did. Is it what you meant? –  Patrick Da Silva Mar 13 '13 at 17:15
@PatrickDaSilva Yes, but remember, there isn’t an itshape environment but an \itshape font switch. A better definition would be \newenvironment{definition}{\textbf{\underline{Definition.}}\vspace{12 pt}\itshape}}{} (no additional group). –  Qrrbrbirlbel Mar 13 '13 at 19:01

In a general setting, you wish to capture the entire environment into a macro, and then pass that macro to another macro as an argument. That is exactly what the environ package provides:

\documentclass{article}
\usepackage{environ}% http://ctan.org/pkg/environ
\NewEnviron{myenv}[1][\textit]{%
#1{\BODY}%
}
\begin{document}
\begin{myenv}
Hello World!
\end{myenv}

\begin{myenv}[\textbf]
Hello World!
\end{myenv}

\begin{myenv}[\slshape]
Hello World!
\end{myenv}

\begin{myenv}[]
Hello World!
\end{myenv}
\end{document}


environ allows you to capture the contents of the environment into the macro \BODY, which is then usable for whatever means. My definition prints it using \textit by default, but you can modify that using a different macro or font switch.

The above definition of myenv is general enough to allow for formatting of the environment content using a macro or a switch (as is done in the 3rd example). Supplying an empty (optional) argument removes any formatting.

-
Interesting package. I don't think I need something so refined to do a simple italic in an environment though, your package seems to be able to do a lot. –  Patrick Da Silva Mar 9 '13 at 3:09
\documentclass{article}
\newenvironment{myenv}{\fontshape{it}\selectfont}{\fontshape{n}\selectfont}
\begin{document}
Before the environment

\begin{myenv}
Testing in the environment
\end{myenv}

Out of the environment
\end{document}

-
Or you could just use \itshape. ;) As the environment itself acts as a group, you don’t need the end part. –  Qrrbrbirlbel Mar 9 '13 at 2:22
Yeah, but I got yelled at in tex.stackexchange.com/questions/99292/… for getting clever with \sc, so I now try to stick to orthodox solutions... especially with Werner on the thread. –  Steven B. Segletes Mar 9 '13 at 2:43
@Steven : I must admit I don't know much about \sc, and something clever is not really relevant to me because I'm asking for something but I want to be able to work with it a little bit, otherwise I don't understand what's going on and I can't adapt your ideas to my work. Codes that "read easily" are more appropriate answers than clever codes that are very intricate... I +1'ed your answer but I prefered Qrrbrbirlbel's. –  Patrick Da Silva Mar 9 '13 at 3:11
@PatrickDaSilva : Understood, and thanks. Here's a good reference for using/changing font attributes: latex-project.org/guides/fntguide.pdf –  Steven B. Segletes Mar 9 '13 at 3:17
@StevenB.Segletes While \sc is a deprecated command, \itshape or \scshape aren't. Using lower level commands such as \fontshape and \selectfont is best avoided if higher level declarations are available. –  egreg Mar 9 '13 at 10:11