%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% LaTeX package underbracket
%
% draws brackets to underline text
% (especially, but not exclusively, syllables of song text)
%
% v1.2 by Bernd Jantzen
% For e-mail contact please use "ctan" before the "@"-sign
% and "bernd-jantzen.de" after it to form my address
% (not given in full clear text here to prevent spam).
% 
% This package draws brackets to underline text (especially,
% but not exclusively, syllables of song text).
% It may be combined with musixtex/musixlyr, within the argument of
% the \setlyrics command or outside the music score.
% It provides several macros to define brackets which may e.g. start
% and end centered below syllables.
% Three length variables are provided to allow a fine adjustment
% of the bracket parameters.
%
% This material is subject to the LaTeX Project Public License.
% See http://www.ctan.org/tex-archive/help/Catalogue/licenses.lppl.html
% for the details of that license.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Commands:
%
% \underbracket{TEXT}
%   draws a bracket underlining TEXT
%   the bracket starts below the left end of TEXT
%   and stops below the right end of TEXT
%
% \Underbracket{TEXT}
%   like \underbracket, but reserves no vertical space below the text
%
% \cunderbracket{LEFT}{TEXT}{RIGHT}
%   draws a bracket underlining LEFTTEXTRIGHT
%   the bracket starts centered below LEFT and stops centered below RIGHT
%
% \cUnderbracket{LEFT}{TEXT}{RIGHT}
%   like cunderbracket, but reserves no vertical space below the text
%
%
% The commands \[c]underbracket do well in normal text which is aligned
% according to the baseline. The vertical space reserved below the text
% guarantees the distance to the next line.
% The commands \[c]Underbracket do well within the \setlyrics command of
% musixlyr where the text is vertically centered between the staves. Vertical
% space reserved below the text would shift the whole text upwards here.
%
%
% Adjustable length parameters:
% (change e.g. with \setlength{\underbracketheight}{4pt})
%
% \underbracketwidth (default 0.3pt):
%   linewidth of the bracket
%
% \underbracketheight (default 3pt):
%   height of the bracket (length of the vertical lines)
%
% \underbracketsep (default 2pt):
%   vertical distance between the lowest part of the text
%   and the upper end of the bracket;
%   also minimal vertical distance below the bracket
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\ProvidesPackage{underbracket}[2004/03/24 v1.2 LaTeX package underbracket]
\NeedsTeXFormat{LaTeX2e}

% declare length variables
\newlength{\underbracketwidth}
\newlength{\underbracketheight}
\newlength{\underbracketsep}
\newlength{\underbracketlength}
\newlength{\underbracketdist}

% need calc pages for adding lengts
\RequirePackage{calc}

% \underbracket{TEXT}
%   draws a bracket underlining TEXT
%   the bracket starts below the left end of TEXT
%   and stops below the right end of TEXT
%
\newcommand{\underbracket}[1]{%
  \settodepth{\underbracketdist}{#1}%
  \addtolength{\underbracketdist}{\underbracketsep+\underbracketheight}%
  \mbox{%
    \rule[-\underbracketdist-\underbracketsep]{0pt}{\underbracketsep}%
    \makebox[0pt][l]{%
      \rule[-\underbracketdist]{\underbracketwidth}{\underbracketheight}}%
    \settowidth{\underbracketlength}{#1}%
    \makebox[0pt][l]{%
      \rule[-\underbracketdist]{\underbracketlength}{\underbracketwidth}}%
    \mbox{#1}%
    \makebox[0pt][r]{%
      \rule[-\underbracketdist]{\underbracketwidth}{\underbracketheight}}%
  }%
}

% \Underbracket{TEXT}
%   like \underbracket, but reserves no vertical space below the text
%
\newcommand{\Underbracket}[1]{%
  \settodepth{\underbracketdist}{#1}%
  \addtolength{\underbracketdist}{\underbracketsep+\underbracketheight}%
  \mbox{%
    \raisebox{0pt}[0pt][0pt]{\makebox[0pt][l]{%
      \rule[-\underbracketdist]{\underbracketwidth}{\underbracketheight}}}%
    \settowidth{\underbracketlength}{#1}%
    \raisebox{0pt}[0pt][0pt]{\makebox[0pt][l]{%
      \rule[-\underbracketdist]{\underbracketlength}{\underbracketwidth}}}%
    \mbox{#1}%
    \raisebox{0pt}[0pt][0pt]{\makebox[0pt][r]{%
      \rule[-\underbracketdist]{\underbracketwidth}{\underbracketheight}}}%
  }%
}

% \cunderbracket{LEFT}{TEXT}{RIGHT}
%   draws a bracket underlining LEFTTEXTRIGHT
%   the bracket starts centered below LEFT and stops centered below RIGHT
%
\newcommand{\cunderbracket}[3]{%
  \settodepth{\underbracketdist}{#1#2#3}%
  \addtolength{\underbracketdist}{\underbracketsep+\underbracketheight}%
  \mbox{%
    \rule[-\underbracketdist-\underbracketsep]{0pt}{\underbracketsep}%
    \settowidth{\underbracketlength}{#1}%
    \makebox[0pt][l]{%
      \hspace*{0.5\underbracketlength}%
      \makebox[0pt]{%
        \rule[-\underbracketdist]{\underbracketwidth}{\underbracketheight}}%
      \rule[-\underbracketdist]{0.5\underbracketlength}{\underbracketwidth}}%
    \mbox{#1}%
    \settowidth{\underbracketlength}{#2}%
    \makebox[0pt][l]{%
      \rule[-\underbracketdist]{\underbracketlength}{\underbracketwidth}}%
    \mbox{#2}%
    \settowidth{\underbracketlength}{#3}%
    \makebox[0pt][l]{%
      \rule[-\underbracketdist]{0.5\underbracketlength}{\underbracketwidth}%
      \makebox[0pt]{%
        \rule[-\underbracketdist]{\underbracketwidth}{\underbracketheight}}}%
    \mbox{#3}%
  }%
}

% \cUnderbracket{LEFT}{TEXT}{RIGHT}
%   like cunderbracket, but reserves no vertical space below the text
%
\newcommand{\cUnderbracket}[3]{%
  \settodepth{\underbracketdist}{#1#2#3}%
  \addtolength{\underbracketdist}{\underbracketsep+\underbracketheight}%
  \mbox{%
    \settowidth{\underbracketlength}{#1}%
    \raisebox{0pt}[0pt][0pt]{\makebox[0pt][l]{%
      \hspace*{0.5\underbracketlength}%
      \makebox[0pt]{%
        \rule[-\underbracketdist]{\underbracketwidth}{\underbracketheight}}%
      \rule[-\underbracketdist]{0.5\underbracketlength}{\underbracketwidth}}}%
    \mbox{#1}%
    \settowidth{\underbracketlength}{#2}%
    \raisebox{0pt}[0pt][0pt]{\makebox[0pt][l]{%
      \rule[-\underbracketdist]{\underbracketlength}{\underbracketwidth}}}%
    \mbox{#2}%
    \settowidth{\underbracketlength}{#3}%
    \raisebox{0pt}[0pt][0pt]{\makebox[0pt][l]{%
      \rule[-\underbracketdist]{0.5\underbracketlength}{\underbracketwidth}%
      \makebox[0pt]{%
        \rule[-\underbracketdist]{\underbracketwidth}{\underbracketheight}}}}%
    \mbox{#3}%
  }%
}

% adjustable length parameters
%
% linewidth of the bracket
\setlength{\underbracketwidth}{0.3pt}
%
% height of the bracket (length of the vertical lines)
\setlength{\underbracketheight}{3pt}
%
% vertical distance between the lowest part of the text
% and the upper end of the bracket;
% also minimal vertical distance below the bracket
\setlength{\underbracketsep}{2pt}

