I've read about defining new, custom environments that can be used like

% do some funky stuff

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


You would create your new environment with the command


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:

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

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


You could define it like this:

  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.

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.