[Tex/LaTex] Defining the own description environment

descriptionenvironmentslists

I need help writing my own description environment. The only thing I want to change is the appearance of description label. I can achieve what I want universally just fine with the following bit in the preamble.

\renewcommand\descriptionlabel[1]{\hspace{\labelsep}\textbf{(#1)}}

However, I just want to use this change in one particular list, not in my entire document. I am not experienced enough in using \newenvironment to do what I want. Please, can you show me what the definition of this new description environment would look like?

Best Answer

To define the new environment just call the original one in it as last and first thing of the 'begin' and 'end' part, respectively. You can place your own redefinition before that. They will be local to this environment. Note that the hash (#) in macro arguments like #1 must be doubled in this case (##1) to differ them for potential arguments of the environment.

\documentclass{article}

\newenvironment{mydescription}{%
   \renewcommand\descriptionlabel[1]{\hspace{\labelsep}\textbf{(##1)}}
   \begin{description}%
}{%
   \end{description}%
}

\begin{document}

\begin{mydescription}
  \item[First] Test
  \item[Second] Test
\end{mydescription}

\begin{description}
  \item[First] Test
  \item[Second] Test
\end{description}

\end{document}

You can also improve this a little and write:

\newcommand\mydescriptionlabel[1]{\hspace{\labelsep}\textbf{(#1)}}
\newenvironment{mydescription}{%
   \let\descriptionlabel\mydescriptionlabel
   \description
}{%
   \enddescription
}

This defines the custom macro outside of the environment so that the hashes aren't a problem which is important in more complex macros. The definition can then be copied using the \let macro. The internal environment is written into the plain form \description ... \enddescription instead of \begin{description} ... \end{description} to avoid confusing error messages when \end{mydescription} is missing. In this case the last \begin would be the one of the internal macro and its name would be printed in the error message instead of the one written in the source file. This isn't a problem for your own small environments, but important if they are used by other people.