14

A friend, who is a LaTeX user but isn't very confident with customising it, told me that he would like to be able to put framed paragraphs and framed equations into his documents. So I took it upon myself to create environments "charlesbox" and "charlesboxmaths" to facilitate doing this. However, I have run into an odd issue with spacing after the maths environment specifically. In this test example, it appears as though LaTeX inserts an additional, completely empty line of text after the \end{charlesboxmaths} line.

I have found that I can suppress this behaviour by eliminating the blank line before the following paragraph; adding a % to the end of the \end{charlesboxmaths} line; or just replacing the \begin and \end commands with the begin- and end-environment code itself. But how can my environment code take care of this problem?

\documentclass[a4paper]{article}

\newsavebox{\mysavebox}

\newlength{\charlesboxdefaultwidth}
\newenvironment{charlesbox}[1][\charlesboxdefaultwidth]%
{\begin{lrbox}{\mysavebox}\begin{minipage}{#1}}%
{\end{minipage}\end{lrbox}\begin{center}\fbox{\usebox{\mysavebox}}\end{center}}

\newenvironment{charlesboxmaths}%
{\begin{lrbox}{\mysavebox}\begin{math}}%
{\end{math}\end{lrbox}\[\fbox{\usebox{\mysavebox}}\]}

\setlength{\charlesboxdefaultwidth}{110mm}

\begin{document}

Lorem ipsum dolor sit amet, pede nunc aliquam. Dapibus urna turpis arcu, venenatis in nam nullam. Erat porttitor laoreet, nulla elit eu, pharetra duis litora ante condimentum aliquet lacus, bibendum lacus et ac eu. Massa mi vel. Metus mauris eu in aenean erat tincidunt, convallis odio, erat dui fermentum suspendisse in, nunc ante morbi vestibulum ullamcorper vel. Torquent vel augue habitasse ipsum egestas, non pede nam.

\begin{charlesbox}
    Possimus pede inceptos vitae amet eu egestas, mi phasellus amet suscipit. Semper vehicula, hymenaeos a dignissim dis, nulla erat. Sed sem cras malesuada. At nonummy, velit nullam, vel ridiculus iaculis perferendis pretium erat a. Sapien sed blandit. Neque risus viverra, sit vitae gravida dignissim sed, ornare vehicula libero, erat accumsan.
\end{charlesbox}

Neque dolor, in odio natoque dolor eget ridiculus, eget quisque quis integer dolor, amet enim, tempus mollis. Dui per, amet quo nulla nisl ligula fusce. Feugiat dui eget nulla ultricies, sem in tincidunt quam, non ac erat. Lacinia cum cursus, eget elit et litora nibh quis sed, diam eros ad sit cras nisl, et pellentesque tincidunt lorem vivamus. Ac tellus phasellus libero, tempor eu, pretium consectetuer quis, dolorum et proin et elit aenean vel.

\begin{charlesboxmaths}
    (x^2 + y^2)^3 = \sin(x)\sin(y) + e^{2xy}
\end{charlesboxmaths}

Vel curae congue a risus sit neque, velit quisque ante in aliquam pulvinar, aliquam interdum id amet dapibus ligula ut, neque eget, nullam risus sapien. Sed sed urna posuere vestibulum. Class felis est sit. Augue turpis dolor molestie, viverra in imperdiet viverra tincidunt faucibus, convallis magna sit, vitae augue tempus in aliquet aliquam. Vel commodo libero quis quam odio. Quia semper vitae velit placerat, lectus amet diam, porta ante taciti per nec, eu hac platea, amet ac venenatis pede.

\end{document}

2 Answers 2

14

Put \ignorespacesafterend at the end of your environment definition; thus

\newenvironment{charlesboxmaths}%
{\begin{lrbox}{\mysavebox}\begin{math}}%
{\end{math}\end{lrbox}\[\fbox{\usebox{\mysavebox}}\]\ignorespacesafterend}
3
  • Thank you very much! If it's not a lot of work, could I ask you to explain for me why "charlesboxmaths" needs this, but "charlesbox" does not?
    – Hammerite
    Jul 3, 2012 at 14:38
  • I don't think I can explain the difference very well! However, if @David Carlisle were to drop by, no doubt he would be able to help. Jul 3, 2012 at 14:50
  • @IanThompson additional answer added (bit too long for a comment:-) Jul 3, 2012 at 16:09
15

enter image description here

The main difference is that LaTeX display environments like center internally use \par so they are in vertical mode after the environment so spaces are ignored even if the space token is not explicitly dropped. However display math is designed to be used mid paragraph and so it immediately returns you to horizontal mode, and if you don't explicitly ignore spaces they will produce an inter-word space.

the image shows the effect of putting the display math in an inner group which stops the following space being seen by \ignorespaces. ccc 4 is out by a space. If you re-insert an ignore spaces after the group things are back to normal, and that is what \ignorespacesafterend does (after a certain sleight of hand to fit in with other processing LaTeX is doing in the end code of an environment).

\documentclass{article}

\begin{document}

\fbox{\parbox{4cm}{%

aaa\begin{center}bbb\end{center}ccc 1

aaa{\begin{center}bbb\end{center}} ccc 2

aaa\[bbb\] ccc 3

aaa{\[bbb\]} ccc 4

aaa{\[bbb\]\aftergroup\ignorespaces} ccc 5

}}

\end{document}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .