~tex-sx/tex-sx/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
\documentclass{article}
%\url{http://tex.stackexchange.com/q/26866/86}
\usepackage{amsmath,tikz}
\usetikzlibrary{matrix,decorations}
\usepackage{calligraphy}

\newlength\mtxrowsep
\setlength\mtxrowsep{1.5ex}
\newlength\mtxcolsep
\setlength\mtxcolsep{2\arraycolsep}

\makeatletter
\pgfdeclaredecoration{doubled lineto}{brace}
{
  \state{brace}[width=+\pgfdecoratedremainingdistance,next state=final]
  {
    \pgfsyssoftpath@setcurrentpath{\pgfutil@empty}
    \pgfpathmoveto{\pgfpointorigin}
    \pgfpathlineto{\pgfqpoint{\pgfdecoratedremainingdistance}{0pt}}
    \pgfpathmoveto{\pgfqpoint{0pt}{\pgfdecorationsegmentamplitude}}
    \pgfpathlineto{\pgfqpoint{\pgfdecoratedremainingdistance}{\pgfdecorationsegmentamplitude}}
  }
  \state{final}{}%
}
\pgfdeclaredecoration{bracket}{brace}
{
  \state{brace}[width=+\pgfdecoratedremainingdistance,next state=final]
  {
    \pgfsyssoftpath@setcurrentpath{\pgfutil@empty}
    \pgfpathmoveto{\pgfpointorigin}
    \pgfpathlineto{\pgfqpoint{0pt}{\pgfdecorationsegmentamplitude}}
    \pgfpathlineto{\pgfqpoint{\pgfdecoratedremainingdistance}{\pgfdecorationsegmentamplitude}}
    \pgfpathlineto{\pgfqpoint{\pgfdecoratedremainingdistance}{0pt}}
  }
  \state{final}{}%
}
\makeatother

\expandafter\def\csname delimiter \string\lbrace\endcsname{calligraphic brace}
\expandafter\def\csname delimiter \string(\endcsname{calligraphic curved parenthesis}
\expandafter\def\csname delimiter \string|\endcsname{lineto}
\expandafter\def\csname delimiter \string\|\endcsname{doubled lineto}
\expandafter\def\csname delimiter \string[\endcsname{bracket}
\expandafter\def\csname delimiter \string\rbrace\endcsname{calligraphic brace}
\expandafter\def\csname delimiter \string)\endcsname{calligraphic curved parenthesis}
\expandafter\def\csname delimiter \string]\endcsname{bracket}

\tikzset{
  ams/.style={
    baseline=-.7ex,
    every delimiter/.style={yshift=-1pt},
    every left delimiter/.style={xshift=2pt},
    every right delimiter/.style={xshift=-2pt},
    every node/.style={inner sep=0pt},
    execute at end picture={
      \path (current bounding box.east) ++(\pgfkeysvalueof{/tikz/ams matrix xsep},0) (current bounding box.west) ++(-\pgfkeysvalueof{/tikz/ams matrix xsep},0);
    },
  },
  ams matrix xsep/.initial={.5ex},
  ams matrix/.style={
    inner sep=1pt,
    column sep=\mtxcolsep,
    row sep=\mtxrowsep,
%    ampersand replacement=\&,
    matrix of math nodes,
  },
  ams delimiters/.style args={#1,#2}{
    left delimiter={#1},
    right delimiter={#2},
  },
  delimiters/.style args={#1,#2}{
    \pgfkeysvalueof{/tikz/matrix delimiter type} delimiters={{#1},{#2}},
  },
  matrix delimiter type/.initial={tikz},
  tikz delimiter style/.style={
    thick,
  },
  tikz delimiters/.style args={#1,#2}{
    left tikz delimiter={#1},
    right tikz delimiter={#2},
  },
  left tikz delimiter/.style={
    render left tikz delimiter/.expand once={\csname delimiter \string#1\endcsname}
  },
  render left tikz delimiter/.style={
    append after command={(\tikzlastnode.north west) edge[tikz delimiter style,decorate,decoration={mirror,#1}] (\tikzlastnode.south west)}
  },
  right tikz delimiter/.style={
    render right tikz delimiter/.expand once={\csname delimiter \string#1\endcsname}
  },
  render right tikz delimiter/.style={
    append after command={(\tikzlastnode.north east) edge[tikz delimiter style,decorate,decoration={#1}] (\tikzlastnode.south east)}
  },
  bmatrix/.style={
    ams,
    every matrix/.style={
      ams matrix,
      delimiters={[,]},
    }
  },
  Bmatrix/.style={
    ams,
    every matrix/.style={
      ams matrix,
      delimiters={\lbrace,\rbrace},
    }
  },
  pmatrix/.style={
    ams,
    every matrix/.style={
      ams matrix,
      delimiters={(,)},
    }
  },
  vmatrix/.style={
    ams,
    every matrix/.style={
      ams matrix,
      delimiters={|,|},
    }
  },
  Vmatrix/.style={
    ams,
    every matrix/.style={
      ams matrix,
      delimiters={\|,\|},
    }
  },
}

\let\matamp=&

\catcode`\&=13
\makeatletter
\def&{\iftikz@is@matrix
  \pgfmatrixnextcell
  \else
  \matamp
  \fi}
\makeatother

%\usepackage{environ}
\def\endtikzmatrix{\\\egroup;\end{tikzpicture}}
\foreach \mtype in {b,B,p,v,V} {

\expandafter\xdef\csname tikz\mtype matrix\endcsname{%
    \noexpand\begin{tikzpicture}[\mtype matrix,matrix delimiter type={tikz}]
    \noexpand\matrix \noexpand\bgroup}
\expandafter\xdef\csname ams\mtype matrix\endcsname{%
    \noexpand\begin{tikzpicture}[\mtype matrix,matrix delimiter type={ams}]
    \noexpand\matrix \noexpand\bgroup}
\expandafter\global\expandafter\let\csname endtikz\mtype matrix\endcsname=\endtikzmatrix
\expandafter\global\expandafter\let\csname endams\mtype matrix\endcsname=\endtikzmatrix
}

\begin{document}
\foreach \mtype in {b,B,p,v,V} {
  \edef\metype{\mtype matrix}
  \edef\tmetype{tikz\mtype matrix}
  \edef\ametype{ams\mtype matrix}
\begin{gather*}
\begin{\tmetype} a & b \\ c & d \end{\tmetype}
\begin{\ametype} a & b \\ c & d \end{\ametype}
\begin{\metype} a & b \\ c & d \end{\metype}
\end{gather*}
}

\end{document}

\begin{gather*}
    4 \times
    \left[\begin{matrix} a & b \\ c & d \end{matrix}\right]
\qquad
    4 \times
    \left[\begin{tikzpicture}
      \matrix [matrix of math nodes, ampersand replacement=\&,
               inner sep=0pt, column sep=\mtxcolsep, row sep=\mtxrowsep]
          { a \& b \\ c \& d \\ };
    \end{tikzpicture}\right]
\qquad
    4 \times
    \left[\begin{minipage}{1.7em}
      {}\hfill\begin{tikzpicture}
        \matrix [matrix of math nodes, ampersand replacement=\&,
                 inner sep=0pt, column sep=\mtxcolsep, row sep=\mtxrowsep]
                { a \& b \\ c \& d \\   };
      \end{tikzpicture}\hfill{}
    \end{minipage}\right]
\qquad 4 \times
\begin{bmatrix}\begin{tikzpicture}
  \matrix [matrix of math nodes, ampersand replacement=\&,
           inner sep=0pt, column sep=\mtxcolsep, row sep=\mtxrowsep]
      { a \& b \\ c \& d \\ };
\end{tikzpicture}\end{bmatrix}
\qquad 4 \times
\left[\vcenter{\hbox{\begin{tikzpicture}
  \matrix [matrix of math nodes, ampersand replacement=\&,
           inner sep=0pt, column sep=\mtxcolsep, row sep=\mtxrowsep]
      { a \& b \\ c \& d \\ };
\end{tikzpicture}}}\right]
\\ 4 \times
\begin{tikzpicture}
%
\path[use as bounding box] (0,0) rectangle (1,1);
\fill[blue] (0,0) circle[radius=2pt];
%shift matrix with respect to bounding box 
\matrix [matrix of math nodes, ampersand replacement=\&,
         inner sep=0pt, column sep=\mtxcolsep, row sep=\mtxrowsep,
         left delimiter={[},right delimiter={]},shift={(0,1)}] (m)
          { a \& b \\ c \& d \\ };
%
\end{tikzpicture}
\qquad 4 \times
\begin{tikzpicture}
%
%shift bounding box
\path[use as bounding box] (0,1) rectangle (1,2);
\fill[blue] (0,0) circle[radius=2pt];
%
          \matrix [matrix of math nodes, ampersand replacement=\&,
               inner sep=0pt, column sep=\mtxcolsep, row sep=\mtxrowsep,
                 left delimiter={[},right delimiter={]}] (m)
          { a \& b \\ c \& d \\ };
%
\end{tikzpicture}
\qquad 4 \times
\begin{tikzpicture}[bmatrix]
\matrix (m) {
  a \& b \\ c \& d \\};
\end{tikzpicture}
\qquad
    4 \times
    \left[\begin{matrix} a & b \\ c & d \end{matrix}\right]
\end{gather*}
\end{document}