Skip to content

A numbered, centered text environment for LaTeX

February 3, 2010

While writing a paper I decided that I need a \LaTeX environment that resembles the usual equation environment, i.e. it takes some content, centers it on it’s own line in the page and then adds, flushed left, a reference number for later use. There might be some ready-made environment or package for doing this, but I couldn’t find one after googling for 20 minutes or so. So I decided to make one which was a nice exercise. I feel like I finally understand the point of the minipage and makebox commands. Here is some code for a new environment that I call numtxt.

\newcounter{txtctr}[section] \setcounter{txtctr}{0}
\newenvironment{numtxt}
	{% This is the begin code
	\refstepcounter{txtctr} \vspace{0.2cm} {\noindent
	(\arabic{section}.\arabic{subsection}.\arabic{txtctr})
	}\hspace{-36pt}\begin{minipage}[c]{\textwidth}\centering}
	{% This is the end code
	\end{minipage} \vspace{0.2cm}}

Basically, I setup a counter (called txtctr, the name stolen from some other bit of macro code), start the environment by displaying the reference number in the format: ( section . subsection . # ). Next we start a minipage which is as wide as the line and center our content inside the minipage. Then to get that minipage aligned right (i.e. to ignore the space taken up by the reference number) I move everything 36 pts to the left. 36 points is about how long the reference number is. There’s probably a better way to do this…

Advertisements
Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: