Skip to content

On a Need to Know Basis

John Cook Wilson

Advanced features of LaTeX are best learned on a need to know basis. The LaTeX Companion can be daunting in its length. If you thought you needed to master all of that material before writing a LaTeX document you would never do so. So start simple, and learn how to do more advanced things as and when you need to.

Learn on a need to know basis, and when you do learn, hack. Don’t feel like you need to know whether the code will work before running it. The worst thing that will happen is that LaTeX will throw an error. LaTeX errors are instructive. Learn to love them.

These reflections arose today when I needed to do something which should have been simple. It was simple—once I learned how to do it. I was preparing a handout for my seminar. On the handout is a summary of the material that I presented on Cook Wilson and the nature of representation and notes on the ensuing discussion. It would be useful if these were visually distinguished.

My first thought was to use the color package. It has a command \colorbox that specifies the background color of a box. However, the results were suboptimal—small strips of white separate the colored lines. Disappointed, I thought, well, perhaps I can just color the relevant text with \textcolor. The discussion however could run for several paragraphs and it turns out that \textcolor chokes when it wraps multiple paragraphs. It was obvious to me, then, that there was no straightforward, out of the box, solution.

I decided to define a new environment, discussion:

\begin{discussion}
    
\end{discussion}

It requires the color package, and you need to define a color that will be used for the background. I used \definecolor{myblue}{rgb}{0.8,0.8,1}. (Readers of the wonderful Tikz Pgf manual will recognize this as the background color of the code blocks.) The strategy was simple—wrap a minipage in a box. The minipage environment defines, well, a small page:

\begin{minipage}[position]{width}
  
\end{minipage}

The minipage is wrapped in a box. Boxes are usually created by commands, but there is a box environment lrbox:

\begin{lrbox}{}
    
\end{lrbox}

The text within this environment is saved in the box <cmd>. Here is the discussion environment with all the bells and whistles.

\makeatletter\newenvironment{discussion}{%
   \noindent\begin{lrbox}{\@tempboxa}\begin{minipage}{\columnwidth}\setlength{\parindent}{1em}}{\end{minipage}\end{lrbox}%
   \colorbox{myblue}{\usebox{\@tempboxa}}
}\makeatother

Here’s the gist:

(Sorry for the duplication, but embedded gists are not showing up in some news readers.)

Two comments. The initial \noindent is required otherwise the entire box will be indented—which would be awkward since the width of the minipage is set to the width of the column. Another issue is that there is no paragraph indentation within the minipage environment. In effect, within that environment, LaTeX sets \parindent to 0. This, however, can be overridden with the \setlength command:

\setlength{\parindent}{1em}

Here is the result: Discussion Environment

{ 2 } Comments

  1. chris | January 26, 2009 at 9:00 am | Permalink

    Great! Thanks for sharing. What typeface do you use in that document? Is it distributed with LaTeX? I like it, especially the italics are very nice.

  2. Mark Eli Kalderon | January 26, 2009 at 11:11 am | Permalink

    The font is Hoefler Text. It is not distributed with LaTeX. I use XeTeX as my engine. It allows you to easily use any font that is installed on your machine to typeset your document.

    My default preamble can be seen here:

    http://gist.github.com/835

    The XeTeX specific commands are on lines 11-17.

Post a Comment

You must be logged in to post a comment.
FireStats icon Powered by FireStats