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've read about defining new, custom environments that can be used like

\begin{thisismyown}
% do some funky stuff
\end{thisismyown}

How do I define thisismyown, and why would I want to?

share|improve this question
add comment

2 Answers

up vote 18 down vote accepted

You would create your new environment with the command

\newenvironment{thisismyown}[num]{begin}{end}

where [num] is the number of arguments you want to pass to the environment, and can be omitted if you don't want any; begin is the text that will be inserted at the point that you call \begin{thisismyown} and correspondingly end is what will appear when you have \end{thisismyown}.

As for why you'd want to, the primary reason is if you have some sort of element that you will repeat multiple times -- theorems, problem statements, solutions, Shakespeare quotes, ... -- and want them all to be formatted in the same way. By putting the formatting into a new environment, you make your document source more readable and can easily change the environment in a single place to update all instances of it throughout your document.

For example, perhaps you are typing up an assignment and want to have an environment to define how your answers will look. You could use something like this:

\newenvironment{question}[2]{
  \noindent\textsc{Question #1}:\begin{quotation}\textit{#2}\end{quotation}
}{
  \hrule\vspace{2em}
}
...
\begin{question}{1}{Solve for $x$.}
  The answer is, $x=42$.
\end{question}

Every question will be nicely formatted to have a label in small caps, numbered by the first argument and restated by the second one. You then place your solution inside the environment, and when it ends you get a solid line across the page and some blank vertical space. Of course, you could also use customized counters to keep track of the question numbering for you, but that's a whole other answer...

share|improve this answer
add comment

You could define it like this:

\newenvironment{thisismyown}{%
  code run at the beginning%
}{%
code run at the end}%

Each time the environment is started the beginning code will run, similar when the environment is closed.

You could also define arguments for your environment:

\newenvironment{thisismyown}[arguments][optional]{beginning code}{end code}

A maximum of 9 arguments can be used. They are accessible in the beginning code by #1 to #9, but not in the end code. So, \newenvironment is similar to \newcommand. But it gives you a scope for the content.

share|improve this answer
add comment

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.