# Leftbar in align environment

We have to mark lines in a LaTeX document by vertical lines in the left margin of the site. An excellent solution outside of align environments is the leftbar environment (framed package):

\begin{leftbar}
Some Text \\ Multiple lines
\begin{align*}
a=b % Even that is working
\end{align*}
\end{leftbar}


But this isn't working inside an align environment:

\begin{align*}
a ={}& b \\
\begin{leftbar}**
c ={}& d \\
e ={}& f \\
\end{leftbar}
g ={}& h
\end{align*}


Has anybode a solution, which is easy to use? Yes, there are some workarounds (as ending the align environment and starting a new one, using phantom to ensure aligning of the "=" and so on) but I need an solution my boss is able to use :-) -- a new leftbar environment or something like that...

-
Welcome to TeX.sx! Your question was migrated here from another stackexchange site. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question. –  Marco Daniel May 14 '12 at 18:39
I have been using the solution from Change bars to indicate location of conditional text. –  Peter Grill May 14 '12 at 18:54
Thanks a lot Peter! Works perfectly –  Elgrimm May 15 '12 at 9:28

## migrated from stackoverflow.comMay 14 '12 at 18:12

This question came from our site for professional and enthusiast programmers.

Maybe a by hand solution can be:

\documentclass{article}
\usepackage{framed}
\usepackage{amsmath}
\begin{document}
\reversemarginpar{\mbox{}\smash{\rule[-1.75cm]{2pt}{0.75cm}}}%
\begin{align*}
a ={}& b \\
c ={}& d \\
e ={}& f \\
g ={}& h
\end{align*}
\end{document}


Or you can use TikZ:

\documentclass{article}
\usepackage{framed}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{tikzpagenodes}

\begin{document}

\begin{align*}
a ={}& b \\
\tikz[remember picture]\node(x-1){}; c ={}& d \\
\tikz[remember picture]\node(x-2){}; e ={}& f \\
g ={}& h
\end{align*}
\tikz[overlay,remember picture]\draw[very thick,red](current page text area.west|-x-1.north)--(current page text area.west|-x-2.south);
\end{document}


EDIT

You can play with the following code. It provides the environment alignrule with one mandatory argument. The arguments set the line with a left bar. (comma separated)

The example:

\documentclass{article}
\usepackage{xcolor}
\usepackage{alignrule}
\usepackage{amsmath}
\begin{document}

\begin{alignrule}{2,4}
%\fboxrule=0.2cm
\begin{align*}
a ={}& b \\
c ={}& d \\
e ={}& f \\
g ={}& h
\end{align*}
\end{alignrule}
\end{document}


The code:

\NeedsTeXFormat{LaTeX2e}[1997/12/01]
\ProvidesPackage{alignrule}
[2011/06/27 v1.7a  multicolumn formatting (FMi)]

\newbox\AR@savetwo
\newbox\AR@saveone
\newif\if@looptest
\newif\if@@par
\newcount\splitlinenumbers@count

\def\split@cmd{%
\@tempdimb\ht\AR@savetwo % height of original box
\setbox\AR@saveone\vsplit\AR@savetwo to\z@ % split it
\setbox\AR@saveone\vbox{\unvbox\AR@saveone}% recover height & depth of \AR@saveone
\setbox\@tempboxa\vbox{\copy\AR@saveone\copy\AR@savetwo}%

% \@tempdima: height of the first line (\AR@saveone)
\setbox\AR@saveone\vbox{\box\AR@saveone}}

\setbox\AR@saveone\vbox{\vskip\@tempdimb\box\AR@saveone}}

% \@tempdima: depth of the first line (\AR@saveone) + fboxsep
\@tempdima\dp\AR@saveone
\dp\AR@saveone\@tempdima}

% \@tempdima: depth of the first line (\AR@saveone) + vertical skip
\@tempdima\dp\AR@saveone
\dp\AR@saveone\@tempdima}

\def\split@cmd{%
\@tempdimb\ht\AR@savetwo % height of original box
\setbox\AR@saveone\vsplit\AR@savetwo to\z@ % split it
\setbox\AR@saveone\vbox{\unvbox\AR@saveone}% recover height & depth of \AR@saveone
\setbox\@tempboxa\vbox{\copy\AR@saveone\copy\AR@savetwo}% naive concatenation
}% gap between two boxes

\def\printline@outrule{%
\hbox to \linewidth{%
\hskip-\fboxrule%
{\vrule \@width\fboxrule\relax}%
\box\AR@saveone%
}%
}

\def\printline@out{%
\hbox to \linewidth{%
\box\AR@saveone%
}%
}

%
\def\endalignrule{\egroup
\ifhmode\par\fi
{\noindent%
\splitlinenumbers@count\@ne%setze \splitlinenumbers@count=1
\@looptesttrue%Schleife lauft, solange @looptesttrue gilt
\baselineskip\z@%
\lineskiplimit\z@
\lineskip\z@%
\vfuzz\maxdimen% Grenzwert, ab dem die Ueberfuellung einer \vbox protokolliert wird.
\split@cmd%
\ifvoid\AR@savetwo      % Only one line
\vbox{%
\hrule\@height\fboxrule%
\printline@out%
\hrule\@height\fboxrule%
}%
}%
\else               % More than one line
\def\print@line{%
\begingroup\edef\x{\endgroup%
\noexpand\in@{,\the\splitlinenumbers@count,}{,\AR@savelines,}}%
\x%
\ifin@
\vbox{\printline@outrule}\hfil%fstln=first line
\else
\vbox{\printline@out}\hfil%fstln=first line
\fi
\loop
\ifvoid\AR@savetwo      % The last line
\begingroup\edef\x{\endgroup%
\noexpand\in@{,\the\splitlinenumbers@count,}{,\AR@savelines,}}%
\x%
\ifin@
\vtop{\printline@outrule}%
\else
\vtop{\printline@out}%
\fi
\else               % 2,...,(n-1)
\begingroup\edef\x{\endgroup%
\noexpand\in@{,\the\splitlinenumbers@count,}{,\AR@savelines,}}%
\x%
\ifin@
\printline@outrule%
\else
\printline@out%
\fi
\fi
\if@looptest\repeat}%
\fi
\leavevmode\print@line\par%
}%
\endgroup\relax%
}

\def\alignrule#1{\par\relax
\begingroup
\abovedisplayskip=\z@
\abovedisplayskip=\z@
\abovedisplayshortskip=\z@
\belowdisplayskip=\z@
\belowdisplayshortskip=\z@
\allowdisplaybreaks
\edef\AR@savelines{#1}
\setbox\AR@savetwo\vbox\bgroup
\hsize\linewidth\@parboxrestore
\parindent2cm\relax}

\endinput

%% Copyright (C) 2012 by Marco Daniel
%% ------------------------------------------------------------------
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.

-
Thx, but the second one is too complicated for my boss... Especially because tikzpagenodes is not part of the miktex packages, which is the distribution given by our IT... The same is true for the first one, which also is not "exact" enough :-( –  Elgrimm May 15 '12 at 8:49
@user1394148: edit –  Marco Daniel May 15 '12 at 16:58

You can use flalign* and any number of \myrules to place vertical rules at the appropriate position:

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\newcommand{\myrule}{\smash{\rule[-.25\baselineskip]{.4pt}{1\baselineskip}}}%
\begin{document}
\begin{align*}
a &= b \\
c &= d \\
e &= f \\
g &= h
\end{align*}

\begin{flalign*}
&& a &= b && \\
\myrule && c &= d && \\
\myrule && e &= f && \\
&& g &= h &&
\end{flalign*}
\end{document}


The flalign environment of amsmath pushes the contents outward. So the idea is to use 3 elements to maintain a centred set of equations and place \myrule where you require a vertical rule on the left.

-
Using flalign instead of align is not possible, because we are only allowed to insert lines of code :-( [The lines which are to be marked with the line are the ones we're inserting] –  Elgrimm May 15 '12 at 8:53