~tex-sx/tex-sx/development

110 by Andrew Stacey (Thargelion)
Removed crazy code from letter shapes, added matrix replacement code for conversion to package
1
\documentclass{article}
2
%\url{http://tex.stackexchange.com/q/26866/86}
3
\usepackage{amsmath,tikz}
4
\usetikzlibrary{matrix,decorations}
5
\usepackage{calligraphy}
6
7
\newlength\mtxrowsep
8
\setlength\mtxrowsep{1.5ex}
9
\newlength\mtxcolsep
10
\setlength\mtxcolsep{2\arraycolsep}
11
12
\makeatletter
13
\pgfdeclaredecoration{doubled lineto}{brace}
14
{
15
  \state{brace}[width=+\pgfdecoratedremainingdistance,next state=final]
16
  {
17
    \pgfsyssoftpath@setcurrentpath{\pgfutil@empty}
18
    \pgfpathmoveto{\pgfpointorigin}
19
    \pgfpathlineto{\pgfqpoint{\pgfdecoratedremainingdistance}{0pt}}
20
    \pgfpathmoveto{\pgfqpoint{0pt}{\pgfdecorationsegmentamplitude}}
21
    \pgfpathlineto{\pgfqpoint{\pgfdecoratedremainingdistance}{\pgfdecorationsegmentamplitude}}
22
  }
23
  \state{final}{}%
24
}
25
\pgfdeclaredecoration{bracket}{brace}
26
{
27
  \state{brace}[width=+\pgfdecoratedremainingdistance,next state=final]
28
  {
29
    \pgfsyssoftpath@setcurrentpath{\pgfutil@empty}
30
    \pgfpathmoveto{\pgfpointorigin}
31
    \pgfpathlineto{\pgfqpoint{0pt}{\pgfdecorationsegmentamplitude}}
32
    \pgfpathlineto{\pgfqpoint{\pgfdecoratedremainingdistance}{\pgfdecorationsegmentamplitude}}
33
    \pgfpathlineto{\pgfqpoint{\pgfdecoratedremainingdistance}{0pt}}
34
  }
35
  \state{final}{}%
36
}
37
\makeatother
38
39
\expandafter\def\csname delimiter \string\lbrace\endcsname{calligraphic brace}
40
\expandafter\def\csname delimiter \string(\endcsname{calligraphic curved parenthesis}
41
\expandafter\def\csname delimiter \string|\endcsname{lineto}
42
\expandafter\def\csname delimiter \string\|\endcsname{doubled lineto}
43
\expandafter\def\csname delimiter \string[\endcsname{bracket}
44
\expandafter\def\csname delimiter \string\rbrace\endcsname{calligraphic brace}
45
\expandafter\def\csname delimiter \string)\endcsname{calligraphic curved parenthesis}
46
\expandafter\def\csname delimiter \string]\endcsname{bracket}
47
48
\tikzset{
49
  ams/.style={
50
    baseline=-.7ex,
51
    every delimiter/.style={yshift=-1pt},
52
    every left delimiter/.style={xshift=2pt},
53
    every right delimiter/.style={xshift=-2pt},
54
    every node/.style={inner sep=0pt},
55
    execute at end picture={
56
      \path (current bounding box.east) ++(\pgfkeysvalueof{/tikz/ams matrix xsep},0) (current bounding box.west) ++(-\pgfkeysvalueof{/tikz/ams matrix xsep},0);
57
    },
58
  },
59
  ams matrix xsep/.initial={.5ex},
60
  ams matrix/.style={
61
    inner sep=1pt,
62
    column sep=\mtxcolsep,
63
    row sep=\mtxrowsep,
64
%    ampersand replacement=\&,
65
    matrix of math nodes,
66
  },
67
  ams delimiters/.style args={#1,#2}{
68
    left delimiter={#1},
69
    right delimiter={#2},
70
  },
71
  delimiters/.style args={#1,#2}{
72
    \pgfkeysvalueof{/tikz/matrix delimiter type} delimiters={{#1},{#2}},
73
  },
74
  matrix delimiter type/.initial={tikz},
75
  tikz delimiter style/.style={
76
    thick,
77
  },
78
  tikz delimiters/.style args={#1,#2}{
79
    left tikz delimiter={#1},
80
    right tikz delimiter={#2},
81
  },
82
  left tikz delimiter/.style={
83
    render left tikz delimiter/.expand once={\csname delimiter \string#1\endcsname}
84
  },
85
  render left tikz delimiter/.style={
86
    append after command={(\tikzlastnode.north west) edge[tikz delimiter style,decorate,decoration={mirror,#1}] (\tikzlastnode.south west)}
87
  },
88
  right tikz delimiter/.style={
89
    render right tikz delimiter/.expand once={\csname delimiter \string#1\endcsname}
90
  },
91
  render right tikz delimiter/.style={
92
    append after command={(\tikzlastnode.north east) edge[tikz delimiter style,decorate,decoration={#1}] (\tikzlastnode.south east)}
93
  },
94
  bmatrix/.style={
95
    ams,
96
    every matrix/.style={
97
      ams matrix,
98
      delimiters={[,]},
99
    }
100
  },
101
  Bmatrix/.style={
102
    ams,
103
    every matrix/.style={
104
      ams matrix,
105
      delimiters={\lbrace,\rbrace},
106
    }
107
  },
108
  pmatrix/.style={
109
    ams,
110
    every matrix/.style={
111
      ams matrix,
112
      delimiters={(,)},
113
    }
114
  },
115
  vmatrix/.style={
116
    ams,
117
    every matrix/.style={
118
      ams matrix,
119
      delimiters={|,|},
120
    }
121
  },
122
  Vmatrix/.style={
123
    ams,
124
    every matrix/.style={
125
      ams matrix,
126
      delimiters={\|,\|},
127
    }
128
  },
129
}
130
131
\let\matamp=&
132
133
\catcode`\&=13
134
\makeatletter
135
\def&{\iftikz@is@matrix
136
  \pgfmatrixnextcell
137
  \else
138
  \matamp
139
  \fi}
140
\makeatother
141
142
%\usepackage{environ}
143
\def\endtikzmatrix{\\\egroup;\end{tikzpicture}}
144
\foreach \mtype in {b,B,p,v,V} {
145
146
\expandafter\xdef\csname tikz\mtype matrix\endcsname{%
147
    \noexpand\begin{tikzpicture}[\mtype matrix,matrix delimiter type={tikz}]
148
    \noexpand\matrix \noexpand\bgroup}
149
\expandafter\xdef\csname ams\mtype matrix\endcsname{%
150
    \noexpand\begin{tikzpicture}[\mtype matrix,matrix delimiter type={ams}]
151
    \noexpand\matrix \noexpand\bgroup}
152
\expandafter\global\expandafter\let\csname endtikz\mtype matrix\endcsname=\endtikzmatrix
153
\expandafter\global\expandafter\let\csname endams\mtype matrix\endcsname=\endtikzmatrix
154
}
155
156
\begin{document}
157
\foreach \mtype in {b,B,p,v,V} {
158
  \edef\metype{\mtype matrix}
159
  \edef\tmetype{tikz\mtype matrix}
160
  \edef\ametype{ams\mtype matrix}
161
\begin{gather*}
162
\begin{\tmetype} a & b \\ c & d \end{\tmetype}
163
\begin{\ametype} a & b \\ c & d \end{\ametype}
164
\begin{\metype} a & b \\ c & d \end{\metype}
165
\end{gather*}
166
}
167
168
\end{document}
169
170
\begin{gather*}
171
    4 \times
172
    \left[\begin{matrix} a & b \\ c & d \end{matrix}\right]
173
\qquad
174
    4 \times
175
    \left[\begin{tikzpicture}
176
      \matrix [matrix of math nodes, ampersand replacement=\&,
177
               inner sep=0pt, column sep=\mtxcolsep, row sep=\mtxrowsep]
178
          { a \& b \\ c \& d \\ };
179
    \end{tikzpicture}\right]
180
\qquad
181
    4 \times
182
    \left[\begin{minipage}{1.7em}
183
      {}\hfill\begin{tikzpicture}
184
        \matrix [matrix of math nodes, ampersand replacement=\&,
185
                 inner sep=0pt, column sep=\mtxcolsep, row sep=\mtxrowsep]
186
                { a \& b \\ c \& d \\   };
187
      \end{tikzpicture}\hfill{}
188
    \end{minipage}\right]
189
\qquad 4 \times
190
\begin{bmatrix}\begin{tikzpicture}
191
  \matrix [matrix of math nodes, ampersand replacement=\&,
192
           inner sep=0pt, column sep=\mtxcolsep, row sep=\mtxrowsep]
193
      { a \& b \\ c \& d \\ };
194
\end{tikzpicture}\end{bmatrix}
195
\qquad 4 \times
196
\left[\vcenter{\hbox{\begin{tikzpicture}
197
  \matrix [matrix of math nodes, ampersand replacement=\&,
198
           inner sep=0pt, column sep=\mtxcolsep, row sep=\mtxrowsep]
199
      { a \& b \\ c \& d \\ };
200
\end{tikzpicture}}}\right]
201
\\ 4 \times
202
\begin{tikzpicture}
203
%
204
\path[use as bounding box] (0,0) rectangle (1,1);
205
\fill[blue] (0,0) circle[radius=2pt];
206
%shift matrix with respect to bounding box 
207
\matrix [matrix of math nodes, ampersand replacement=\&,
208
         inner sep=0pt, column sep=\mtxcolsep, row sep=\mtxrowsep,
209
         left delimiter={[},right delimiter={]},shift={(0,1)}] (m)
210
          { a \& b \\ c \& d \\ };
211
%
212
\end{tikzpicture}
213
\qquad 4 \times
214
\begin{tikzpicture}
215
%
216
%shift bounding box
217
\path[use as bounding box] (0,1) rectangle (1,2);
218
\fill[blue] (0,0) circle[radius=2pt];
219
%
220
          \matrix [matrix of math nodes, ampersand replacement=\&,
221
               inner sep=0pt, column sep=\mtxcolsep, row sep=\mtxrowsep,
222
                 left delimiter={[},right delimiter={]}] (m)
223
          { a \& b \\ c \& d \\ };
224
%
225
\end{tikzpicture}
226
\qquad 4 \times
227
\begin{tikzpicture}[bmatrix]
228
\matrix (m) {
229
  a \& b \\ c \& d \\};
230
\end{tikzpicture}
231
\qquad
232
    4 \times
233
    \left[\begin{matrix} a & b \\ c & d \end{matrix}\right]
234
\end{gather*}
235
\end{document}