% !TeX spellcheck = en_US
% !TeX root = tikz-ext-manual.tex
% Copyright 2022 by Qrrbrbirlbel
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Free Documentation License.
%

\section{Shape: Uncentered Rectangle}
\begin{pgflibrary}{ext.shapes.uncenteredrectangle}
  A rectangle that has a variable horizontal center with three node parts.
  \inspiration{UncRectCD-Q,UncRectForest-Q}{UncRectCD-A,UncRectForest-A}
\end{pgflibrary}
\begin{shape}{uncentered rectangle}

For some alignment problems, this shape could be useful.

It has three node parts: the standard |text| part,
the |left| part that is to the left of |text|
and the |right| part that is to the right of |text|.

When edges are to be connected with this shape, the
following key changes to which inner center this shape will
calculate the appropriate point on the border.
\begin{key}{/pgf/uncentered rectangle center=\meta{left}\textrm{ or }\meta{text}\textrm{ or }\meta{right}\textrm{ or }\meta{real} (initially text)}
  Sets the center that is to be used for connecting edges.
  
  This will also move the anchors |north|, |mid|, |base| and |south| along.
  In the picture below, this are marked red.
\end{key}

For support of the \referenceLibraryandIndexO{cd} library of the |tikz-cd| package,
this shape also supports a dynamic $y$ value for its anchors |center|, |west| and |east|.
\begin{key}{/pgf/uncentered rectangle center yshift=\meta{dimension} (initially \{\})}
  This determines the distance between the baseline and the |center| anchors.
  
  If \meta{dimension} is empty, the real center will be used which is the default.
  
  For use with |cd|, set this to |axis_height|.
  Due to a bug with \referenceKeyandIndexO{execute at end node}
  this needs a lot of fixing to be able to use in a commutative diagram, though.

\tikzexternaldisable
\catcode`\|=12
\begin{codeexample}[leave comments, preamble=\usetikzlibrary{cd, ext.shapes.uncenteredrectangle}]
\makeatletter
\tikzcdset{
  every diagram/.append style={
    /pgf/uncentered rectangle center=text,
    /tikz/math align/.style={
      shape=uncentered rectangle,
      /pgf/uncentered rectangle center yshift=axis_height,
      commutative diagrams/math mode=false},
    /tikz/math align left/.style={
      math align,
      /utils/exec={\def\mathalign####1=####2;{$####2$\nodepart{left}$####1={}$}}},
    /tikz/math align right/.style={
      math align,
      /utils/exec={\def\mathalign####1=####2;{$####1$\nodepart{right}${}=####2$}}},
    /tikz/matrix of math nodes/.style={
      matrix of nodes,
      nodes={
        execute at begin node=\iftikzcd@mathmode$\fi,
        execute at end node  =\iftikzcd@mathmode$\fi}}}}
\makeatother
\tikzcdset{install C shortcut/.code=\newcommand*\C[1]{C_{\%_{##1}}}}
\begin{tikzcd}[
  install C shortcut,
  sep=tiny,
  arrows={-, gray},
  cells={font=\strut, inner xsep=.2ex, inner ysep=.1ex}
]
\C{1} \drar &         & |[math align right]| \mathalign m_{r_1} = \C{2}-C_\%; \dlar\\
            & C_\% \\
\C{2} \urar &         & |[math align right]| \mathalign m_{r_2} = \C{1}-C_\%; \ular
\end{tikzcd}
\end{codeexample}
\end{key}
\pagebreak
\begin{codeexample}[preamble=\usepgflibrary{ext.shapes.uncenteredrectangle}]
\begin{tikzpicture}[style north/.style=red, style south/.style=red, style center/.style=red, style base/.style=red, style mid/.style=red]
\Huge
\node[shape example, name=n, uncentered rectangle]
  {centered \nodepart{left} Un \nodepart{right} \space Rectangle\vrule width 1pt height 2cm}
  foreach \anchor/\pos in {
   north west/above left, north/below, north east/above right, real north/above,  left north/above, right north/above, text north/above,
         west/left,      center/above,       east/right,       real center/above, left center/above,right center/above,text center/below,
     mid west/left,         mid/left,    mid east/right,       real mid/above,    left mid/above,   right mid/above,   text mid/above,
    base west/left,        base/right,  base east/right,       real base/below,   left base/below,  right base/below,  text base/below,
   south west/below left, south/above, south east/below right, real south/below,  left south/below, right south/below, text south/below,
                             10/right,        130/below,                          left/left,        right/right,       text/right}{
    plot[mark=x, only marks] coordinates {(n.\anchor)}
    node[inner sep=.1em, style \anchor/.try, style/.expand once=\pos] {\tiny\ttfamily\anchor}};
\end{tikzpicture}
\end{codeexample}
\end{shape}
\endinput