Jul 04

Changing parts of already existing commands and environments in LaTeX

In using the exam document class, I thought it would be nice to be able to include a new page at the end of every solution. The solutions I had in this particular document were long, and to separate the solutions from the next questions, I thought this would be nice. The nicest way would be to redefine the solution environment to include a \newpage at the end…but then wouldn’t I have to know the entire definition of the solution enviroment and do it exactly the same? Seems there should be a better way…and there is.


Nice and short and easy. \endsolution is a command that returns what is in the end of the solution environment.  It seems that this works with any environment, that is, if you define a new environment \newenvironment{NAME}{START}{END} then \endNAME will return END.  Similarly, using the name of the environment as a command gets the beginning: \NAME will return START.

Similarly, you can define a command that uses itself. This is referred to as “patching exiting commands” in the UK List of TeX Frequently Asked Questions. It’s pretty easy for basic commands, but using the exam documentclass, lets say you wanted to add the words “THIS ONE IS HARD” to every question on an exam. Apparently (as explained in the above link) due to the fact that the question command has an optional parameter, you must do the following: first, in your preamble, type


Then, put this code inside your question environment:

{\oldquestion[{#1}] \textbf{THIS ONE IS HARD!}}


Here is a sample tex file showing how to do this in a more general context:

\newenvironment{test}{\begin{center}\bfseries %
\parindent 0in
Here is test environment number 1:
This is the first test environment
Here is \verb"\test": \fbox{\meaning\test}
Here is \verb"\endtest": \fbox{\meaning\endtest}
I will redefine the test environment to finish with some text.
\def\endtest{\textit{\Large ...and this is at the end!}\oldendtest}
Here is test environment number 2:
This is the second test environment
Here is \verb"\test": \fbox{\meaning\test} (unchanged)
Here is \verb"\endtest": \fbox{\meaning\endtest}
Here is \verb"\oldendtest": \fbox{\meaning\oldendtest}
Lets update the section break command...
\renewcommand{\mysectionbreak}{\oldsectionbreak[1cm] \oldsectionbreak[1in] \oldsectionbreak[1cm]}
Done. Let's do it..
Here is \verb"\mysectionbreak": \fbox{\meaning\mysectionbreak}
Here is \verb"\oldsectionbreak": \fbox{\meaning\oldsectionbreak}
Now lets make the environment enclosed in a box and add some spacing before and after it.
Here is test environment number 3:
This is the third test environment
Here is \verb"\test": \fbox{\meaning\test}
Here is \verb"\endtest": \fbox{\meaning\endtest}

Enjoy and keep learning! -Rob

1 comment

  1. Howard

    When I first started blogging in 2002 social media existed almost soley for the purpose of anonymously sharing the details of your life with kindred strangers that you would eventually build friendships with. It has changed completely but I continue to use it as I always did by keeping my personal sites private (no sharing of my real name, location or details) but also getting the most from new social media which is extension of the relationships I have in Real Life.


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

What is 8 + 8 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)