12

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}
}
2

3 Answers 3

11

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.

4
  • Thanks! Actually I chose the \begin{itshape}/\end{itshape} first and it work just fine... what exactly is wrong with it? Mar 9, 2013 at 3:08
  • 2
    @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.
    – cgnieder
    Mar 9, 2013 at 8:25
  • I edited my question to show what I actually did. Is it what you meant? Mar 13, 2013 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). Mar 13, 2013 at 19:01
11

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:

enter image description here

\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.

5
  • 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. Mar 9, 2013 at 3:09
  • What if you want to use \begin{verbatim}? Then \BODY won't work...
    – Someone
    Apr 14, 2020 at 15:11
  • @Someone True, but is it necessary in your instance? Do you have bizarre symbols you want within the output? There are some alternatives, but it may depend on your actual use-case.
    – Werner
    Apr 14, 2020 at 21:45
  • I wanted to put verbatim text into a colored background.
    – Someone
    Apr 22, 2020 at 21:14
2
\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}
6
  • 4
    Or you could just use \itshape. ;) As the environment itself acts as a group, you don’t need the end part. Mar 9, 2013 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. Mar 9, 2013 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. Mar 9, 2013 at 3:11
  • @PatrickDaSilva : Understood, and thanks. Here's a good reference for using/changing font attributes: latex-project.org/guides/fntguide.pdf Mar 9, 2013 at 3:17
  • 2
    @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, 2013 at 10:11

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.