有没有一种简单的方法可以告诉 TikZ 我想要一条垂直线来分隔矩阵的列?我知道如何手动绘制它们,我正在寻找一种自动解决方案,也许使用样式或其他东西。
答案1
您可以使用“节点矩阵”和锚点:
\begin{tikzpicture}
\matrix (mat) [matrix of nodes]
{
1 & 2 & 3 \\
1 & 2 & 3 \\
1 & 2 & 3 \\
1 & 2 & 3 \\
};
% Matrix is called 'mat' first number is row, second numer is column
\draw[thick,red] (mat-1-1.north east) -- (mat-4-1.south east);
\end{tikzpicture}
答案2
这是一个包含数学节点矩阵的示例,其中包含分隔符和分隔线,并进行了一些移动。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of math nodes,left delimiter=(,right delimiter=),
inner sep=2pt,outer sep=0pt]{
a & b & [1em] 0 \\
c & d & 1 \\ };
\draw ([xshift=0.5em]m-1-2.north east) -- ([xshift=0.5em]m-2-2.south east);
\end{tikzpicture}
\end{document}
如果条目的宽度不同,则可以使用|-
而不是--
来获取直线,例如
\draw ([xshift=0.5em]m-1-2.north east-|m-2-2.south east) -|
([xshift=0.5em]m-2-2.south east);
甚至更好:为矩阵中的节点设置最小宽度属性。
答案3
以下是根据以下答案得出的答案使用 TiKZ 数学节点矩阵模拟 amsmath 矩阵的障碍和如何在 Tikz 中设置矩阵节点的行和列的背景颜色?你没有在问题中指定你想要排版的矩阵类型,所以我只是选择了与 Stefan 和 xeroc 使用的矩阵类似的矩阵。你到底需要什么代码,很大程度上取决于你的矩阵代码是什么样子。你真正需要的唯一代码是来自如何在 Tikz 中设置矩阵节点的行和列的背景颜色? 其他的东西都是为了让它看起来更令人印象深刻。
如果代码来自如何在 Tikz 中设置矩阵节点的行和列的背景颜色?位于一个包中(下面称为tikzamsmatrix
),该文档将如下所示:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,decorations}
\usepackage{calligraphy}
\usepackage{matrixcells}
\usepackage{tikzamsmatrix}
\begin{document}
\[
\begin{tikzamatrix}
a & b & 0 \\
c & a + c & {\displaystyle \int_0^1 \sin(x) d x}
\end{tikzamatrix}
\]
\end{document}
并会产生:
由于该问题尚未包含在包中,因此代码实际上如下所示:
\documentclass{article}
%\url{https://tex.stackexchange.com/q/9492/86}
\usepackage{amsmath,tikz}
\usetikzlibrary{matrix,decorations}
\usepackage{calligraphy}
\usepackage{matrixcells}
\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
}
\def\tikzamatrix{\begin{tikzpicture}[pmatrix,matrix delimiter type={tikz}]\matrix[label cells,name=am]\bgroup}
\def\endtikzamatrix{\\\egroup;\draw (am-cell-1-\the\pgfmatrixcurrentcolumn.north west) -- (am-cell-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.south west);\end{tikzpicture}}
\begin{document}
\begin{tikzamatrix}
a & b & 0 \\
c & a + c & {\displaystyle \int_0^1 \sin(x) d x}
\end{tikzamatrix}
\end{document}
(书法和矩阵细胞包可从TeX-SX 启动板页面。
答案4
即使你要求 TikZ 解决方案,这里还有另一种方法,nicematrix
扩大。
因此,正如@Caramdir 指出的那样,无论矩阵中有什么,线条都会自动调整。
此外,您还可以使用 TikZ 命令来调整矩阵内的单元格。请查看文档!
\documentclass{article}
\usepackage{nicematrix}
\DeclareRobustCommand{\dder}{\mathop{}\mathopen{}\mathrm{d}}
\begin{document}
$\begin{pNiceArray}{c|c|c}
1 & \int_{0}^{\infty}2x \dder x & 3 \\
1 & 2 & 3 \\
1 & 2+3+4 & 3 \\
1 & 2 & 3 \\
\end{pNiceArray}$
\end{document}