对齐环境中的左栏

对齐环境中的左栏

我们必须在网站左边用垂直线标记 LaTeX 文档中的行。一个很好的解决方案外部环境alignleftbar环境(framed包):

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

但这是行不通的里面一个align环境:

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

有人有易于使用的解决方案吗?是的,有一些解决方法(例如结束环境align并开始一个新环境,使用幻像来确保“=”对齐等等),但我需要我的老板能够使用的解决方案:-)——一个新的leftbar环境或类似的东西......

答案1

也许手动解决方案可以是:

\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}

或者你可以使用 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}

编辑


你可以使用以下代码。它为环境提供了alignrule一个强制参数。参数设置了带有左栏的行。(逗号分隔)

这个例子:

\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}

在此处输入图片描述

代码:

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

答案2

您可以使用flalign*任意数量的\myrules 将垂直规则放置在适当的位置:

在此处输入图片描述

\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}

环境flalignamsmath将内容向外推。因此,我们的想法是使用 3 个元素来维持一组居中的方程式,并将其放置\myrule在左侧需要垂直规则的位置。

相关内容