5

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...

migrated from stackoverflow.com May 14 '12 at 18:12

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

6

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}

enter image description here

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
   \advance\@tempdimb\dp\AR@savetwo 
   \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}%
   \advance\@tempdimb-\ht\@tempboxa 
   \advance\@tempdimb-\dp\@tempboxa}%



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

\def\addskipht{%
     \setbox\AR@saveone\vbox{\vskip\@tempdimb\box\AR@saveone}}

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

% \@tempdima: depth of the first line (\AR@saveone) + vertical skip
\def\@addskipdp{%
     \@tempdima\dp\AR@saveone
     \advance\@tempdima\@tempdimb
     \dp\AR@saveone\@tempdima}



\def\split@cmd{%
    \@tempdimb\ht\AR@savetwo % height of original box
    \advance\@tempdimb\dp\AR@savetwo 
    \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
    \advance\@tempdimb-\ht\@tempboxa 
    \advance\@tempdimb-\dp\@tempboxa%
}% 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%
    \@addfsepht%
    \@addskipdp%
    \ifvoid\AR@savetwo      % Only one line
        \def\print@line{\@addfsepdp%
          \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
        \advance\splitlinenumbers@count\@ne
        \loop 
          \split@cmd\@addskipdp\leavevmode
          \ifvoid\AR@savetwo      % The last line
              \@looptestfalse\@addfsepdp
           \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
          \hfil\advance\splitlinenumbers@count\@ne%
        \if@looptest\repeat}%
    \fi
    \leavevmode\print@line\par%
   }%
   \endgroup\relax%
}

\def\alignrule#1{\par\relax
\addvspace{\abovedisplayskip}
\begingroup
\abovedisplayskip=\z@
\abovedisplayskip=\z@
\abovedisplayshortskip=\z@
\belowdisplayskip=\z@
\belowdisplayshortskip=\z@
\allowdisplaybreaks
\edef\AR@savelines{#1}
\setbox\AR@savetwo\vbox\bgroup
\advance\linewidth -2\fboxrule
\advance\linewidth -2\fboxsep
\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
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   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
2

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

enter image description here

\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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.