Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am fairly new to LaTeX, so I apologize if this is a beginner question, but I have not found a good way to query for it otherwise.

I have observed that when I use an environment like \begin{itemize}...\end{itemize}, I can place \newcommand inside the environment, which are scoped to the environment.

\documentclass{article}

\begin{document}
\begin{itemize}

\newcommand{\FFF}{\epsilon}

\item $\FFF$

\end{itemize}
%$\FFF$ %this is out of scope, so it would cause an error if it were commented back in.

\end{document}

However, environments like itemize and enumerate tend to have special effects (such as giving meaning to \item). My question is whether there exists an environment which has no "special effects", whose only role is to limit the scope of declarations.

share|improve this question
5  
You can enclose anything in braces {...} and this will limit the scope macro definitions within the braces. –  Alan Munn Feb 25 '13 at 4:36
    
I accept this answer if you write it as one, but just out of curiosity, does this mean all LaTeX environments come with built-in {...}? –  merlin2011 Feb 25 '13 at 4:40
    
@merlin2011 Essentially yes; LaTeX environments have their own scope. Anything defined (or renewed) inside of them won't be available outside of the environment. –  Sam Whited Feb 25 '13 at 4:43

1 Answer 1

up vote 15 down vote accepted

As Alan Munn said, using braces is one way to go. You an also simply define your own environment:

\newenvironment*{dummyenv}{}{}

And then use it like you normally would:

\begin{dummyenv}
  \newcommand*{\scopedcommand}{Do stuff}
  \scopedcommand % This works
\end{dummyenv}

The two arguments to newenvironment are the code to insert before the content of the environment, and after. Leaving them blank essentially defines an environment that does nothing.

Therefore, the following won't compile:

\documentclass[a4paper]{article}                                                 

\newenvironment*{dummyenv}{}{}                                                   

\begin{document}                                                                 
  \begin{dummyenv}                                                               
    \newcommand*{\dummycmd}{This is scoped}                                      
    \dummycmd                                                                    
  \end{dummyenv}                                                                 
  \dummycmd                                                                     
\end{document}

Unless you comment out the second dummycmd.

EDIT: Also, meant to say: You can also use the TeX primitives \begingroup and \endgroup for this. They form what TeX calls a 'semi-simple' group, and are not interchangeable with {...}.

EDIT 2: As @tohecz reminded me in the comments, the correct way to do this (if you really want an environment for whatever reason) is to use

\begin{@empty}
\end{@empty}

Any of the other solutions will work too, however, this is cleaner than defining a new environment.

share|improve this answer
1  
You can use \begin{@empty} for this. Just remember that you cannot use \begin{relax} because \relax behaves like a non-defined thingy in these contexts ;) –  yo' Feb 25 '13 at 23:27
    
@tohecz Absolutely right; I forgot all about @empty. I'll update the answer — thanks! –  Sam Whited Feb 25 '13 at 23:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.