Adjusting vertical spacing of some display equations

I am using the package changebar to set off a couple of environments in my document.

When I end an environment with a displayed equation, the changebar extends too far past the equation. I'd like to have the changebar stop at the baseline of the displayed equation.

My MWE...

\documentclass{article}
\usepackage{amsmath}
\usepackage[leftbars]{changebar}
\setlength{\changebarsep}{-20pt}
\setlength{\changebarwidth}{2pt}
\newcounter{example}
\newenvironment{exampl}
{
\par\vspace{10pt}
\begin{enumerate}
\item[]
\refstepcounter{example}
\noindent
\cbstart
\textbf{Example \theexample} %
}
{
\cbend
\end{enumerate}
}
\begin{document}
In Example 1 below, the bar extends to the baseline of the ending text.
\begin{exampl}
$c^2=a^2+b^2$
generalizes to the Law of Cosines
$c^2=a^2+b^2-2ab\cos C\ .$
This generalization shows how to use the Pythagorean Theorem with oblique triangles; that is, with non-right triangles.
\end{exampl}

However, Example 2 ends with a display equation and the bar extends past the baseline of the equation.
\begin{exampl}
$c^2=a^2+b^2$
generalizes to the Law of Cosines
$c^2=a^2+b^2-2ab\cos C\ .$
\end{exampl}

How can I get the bar to stop at the baseline of the display equation in Example 2?
\end{document}

I've tried adjusting \belowdisplayskip, but I don't know how to affect only the display equations at the end of my environment.

I appreciate any help and comments.

This answer is specifically to address this comment. First, let me show some code and a picture.

Code and picture

\documentclass{article}
\usepackage{amsmath}
\usepackage[margin=2.5in]{geometry}
\usepackage[leftbars]{changebar}

\setlength{\changebarwidth}{2pt}
\setlength{\changebarsep}{0pt}
\setlength{\parindent}{0pt}

\begin{document}

\cbstart
First we wrap the entire paragraph, including the math in the changebar.
$E = mc^2$
Some text.
\cbend

Next, we show what happens if we wrap an equation by itself in change bar.

\cbstart
$E = mc^2$
\cbend

In the next example the changebar commands are issued from \emph{within} the display math mode.

$\cbstart E = mc^2 \cbend$

In this example, we wrap the equations in some text (as it would appear within a paragraph), but only issue the changebar commands immediately before and immediately after the equation.
\cbstart
$E = mc^2$
\cbend
some more text.

In this final example, we see what happens if we enter vertical mode before passing control to the changebar end command.
\cbstart
$E =mc^2$\par
\cbend

Some more text so the spacing can be more clearly seen. Just extend this a little bit more.

\end{document}

Discussion

The key is that the \cbstart and \cbend commands behave differently when in vertical or horizontal mode. In vertical mode \cbend marks the vertical position and that's that. In horizontal mode, however, it wraps in \vadjust, so that the current line gets processed first before putting down the mark. The problem with this is that on an otherwise empty line, \cbend and \leavevmode\cbend behaves differently: the latter will first type-set a blank line and then end the changebar, resulting in one extra line of space.

If you want it flush with the baseline of the displayed equation, you cannot do it, at least not the way you wrap things in an environment. To access the vertical position of the baseline of the displayed equation you really have to place \cbend inside the final displayed equation. I suppose there may be ways to do it (similar to \qedhere for the proof environments, I expect), but is too much for me to handle.

But if you are happy with a solution that removes only the extra space generated from the discussion above (effectively due to \cbend setting an extra blank line), what you can do is to make sure that \cbend is called in vertical mode (and not horizontal mode). The command to do that is \par.

So what you can do is replace, in your definition of the environment, the call to \cbend by \par\cbend and that should remove some (but not all) of the extra space.

• I had discovered that if I didn't have an environment, I could place the \cbend at the end of the final displayed equation. However, using the environment caused trouble. I appreciate your answer to my question. Jul 30 '16 at 0:26

Why use changebar? If you are generating a list of examples, it seems easier to use mdframed + ntheorem. See the documentation for mdframed for usage information. For example:

\documentclass{article}
\usepackage{amsmath}
\usepackage{ntheorem}
\usepackage[xcolor,tikz]{mdframed}

\theoremstyle{plain}
\theorembodyfont{\upshape}
\newmdtheoremenv[%
innertopmargin=0pt,% make the frame "tight"
innerbottommargin=0pt,%
rightline=false,% Kill all the lines except the left one
topline=false,%
bottomline=false,%
linecolor=gray,%
linewidth=4pt,%
leftmargin=10pt%
ntheorem = true% since we are using ntheorem to configure the style
]{exampl}{Example}

\begin{document}
In Example 1 below, the bar extends to the baseline of the ending text.
\begin{exampl}
$c^2=a^2+b^2$
generalizes to the Law of Cosines
$c^2=a^2+b^2-2ab\cos C\ .$
This generalization shows how to use the Pythagorean Theorem with oblique triangles; that is, with non-right triangles.
\end{exampl}

However, Example 2 ends with a display equation and the bar extends past the baseline of the equation.
\begin{exampl}
$c^2=a^2+b^2$
$c^2=a^2+b^2-2ab\cos C\ .$%