tikz 中的花括号调整?

tikz 中的花括号调整?

我正在使用这个的帮助回答对于我的写作来说,我对 Tikz 包没有太多的了解。 在此处输入图片描述

我需要在上图中做以下调整:

  1. 花括号向下移动
  2. 花括号之间的小间隙

任何帮助,将不胜感激。

\documentclass[11pt,twoside,a4paper]{article}
\usepackage{braket,amsmath,amssymb}
\usepackage{tikz}
 \usetikzlibrary{matrix,decorations.pathreplacing, calc, positioning}

\begin{document}
\begin{equation}  
 Q=\begin{tikzpicture}[baseline=(m-3-1.base)]
\matrix [matrix of math nodes,left delimiter=(,right delimiter=),row sep=0.5cm,column sep=0.5cm] (m) {
1&2&3&4&3&4 \\
1&2&3&4&3&4 \\
1&2&3&4&3&4 \\
1&2&3&4&3&4 \\
1&2&3&4&3&4 \\
1&2&3&4&3&4 \\};

\draw[solid] ($0.5*(m-1-2.north east)+0.5*(m-1-3.north west)$) --
     ($0.5*(m-6-3.south east)+0.5*(m-6-2.south west)$);
\draw[solid] ($0.5*(m-1-4.north east)+0.5*(m-1-5.north west)$) --
     ($0.5*(m-6-5.south east)+0.5*(m-6-4.south west)$);

\draw[solid] ($0.5*(m-2-1.south west)+0.5*(m-3-1.north west)$) --
 ($0.5*(m-2-6.south east)+0.5*(m-3-6.north east)$);

\draw[solid] ($0.5*(m-4-1.south west)+0.5*(m-5-1.north west)$) --
 ($0.5*(m-4-6.south east)+0.5*(m-5-6.north east)$);

\node[above=10pt of m-1-1] (top-1) {$q_s$};
\node[above=10pt of m-1-2] (top-2) {$p_s$};
\node[above=10pt of m-1-3] (top-3) {$q_{m_1}$};
\node[above=10pt of m-1-4] (top-4) {$p_{m_1}$};
\node[above=10pt of m-1-5] (top-5) {$q_{m_2}$};
\node[above=10pt of m-1-6] (top-6) {$p_{m_2}$};




\node[rectangle,above delimiter=\{] (del-top-1) at ($0.5*(top-1.south) +0.5*(top-2.south)$) {\tikz{\path (top-1.south west) rectangle (top-2.north east);}};
\node[above=10pt] at (del-top-1.north) {System};

\node[rectangle,above delimiter=\{] (del-top-2) at ($0.5*(top-3.south) +0.5*(top-4.south)$) {\tikz{\path (top-3.south west) rectangle (top-4.north east);}};
\node[above=10pt] at (del-top-2.north) {Meter 1};

\node[rectangle,above delimiter=\{] (del-top-3) at ($0.5*(top-5.south) +0.5*(top-6.south)$) {\tikz{\path (top-5.south west) rectangle (top-6.north east);}};
\node[above=10pt] at (del-top-3.north) {Meter 2};


\node[right=12pt of m-1-6] (left-1) {$q_s$};
\node[right=12pt of m-2-6] (left-1) {$p_s$};
\node[right=12pt of m-3-6] (left-1) {$q_{m_1}$};
\node[right=12pt of m-4-6] (left-1) {$p_{m_1}$};
\node[right=12pt of m-5-6] (left-1) {$q_{m_2}$};
\node[right=12pt of m-6-6] (left-1) {$p_{m_2}$};



\end{tikzpicture}
\end{equation}


\end{document}

答案1

由于您已在加载decorations.pathreplacing,因此可以使用它来绘制括号。我将它们向下移动并在它们之间添加了一个小间隙。我还使用了 和 的范围,text width使text depth事物更加对称。

\documentclass[11pt,twoside,a4paper]{article}
\usepackage{braket,amsmath,amssymb}
\usepackage{tikz}
 \usetikzlibrary{matrix,decorations.pathreplacing, calc, positioning}

\begin{document}
\begin{equation}  
 Q=\begin{tikzpicture}[baseline=(m-3-1.base)]
\matrix [matrix of math nodes,left delimiter=(,right delimiter=),row sep=0.5cm,column sep=0.5cm] (m) {
1&2&3&4&3&4 \\
1&2&3&4&3&4 \\
1&2&3&4&3&4 \\
1&2&3&4&3&4 \\
1&2&3&4&3&4 \\
1&2&3&4&3&4 \\};

\draw[solid] ($0.5*(m-1-2.north east)+0.5*(m-1-3.north west)$) --
     ($0.5*(m-6-3.south east)+0.5*(m-6-2.south west)$);
\draw[solid] ($0.5*(m-1-4.north east)+0.5*(m-1-5.north west)$) --
     ($0.5*(m-6-5.south east)+0.5*(m-6-4.south west)$);

\draw[solid] ($0.5*(m-2-1.south west)+0.5*(m-3-1.north west)$) --
 ($0.5*(m-2-6.south east)+0.5*(m-3-6.north east)$);

\draw[solid] ($0.5*(m-4-1.south west)+0.5*(m-5-1.north west)$) --
 ($0.5*(m-4-6.south east)+0.5*(m-5-6.north east)$);

\begin{scope}[nodes={text width=1.1em,align=center}]
 \node[above=10pt of m-1-1] (top-1) {$q_s$};
 \node[above=10pt of m-1-2] (top-2) {$p_s$};
 \node[above=10pt of m-1-3] (top-3) {$q_{m_1}$};
 \node[above=10pt of m-1-4] (top-4) {$p_{m_1}$};
 \node[above=10pt of m-1-5] (top-5) {$q_{m_2}$};
 \node[above=10pt of m-1-6] (top-6) {$p_{m_2}$};
\end{scope}

\coordinate[above=3pt of top-1] (aux);
\begin{scope}[nodes={text depth=0.25ex}]
 \draw[thick,decorate,decoration=brace] (top-1.west|-aux) -- (top-2.east|-aux)
  node[midway,above=0.1ex] {System};
 \draw[thick,decorate,decoration=brace] (top-3.west|-aux) -- (top-4.east|-aux)
  node[midway,above=0.1ex] {Meter 1};
 \draw[thick,decorate,decoration=brace] (top-5.west|-aux) -- (top-6.east|-aux)
  node[midway,above=0.1ex] {Meter 2};
\end{scope}


\node[right=12pt of m-1-6] (left-1) {$q_s$};
\node[right=12pt of m-2-6] (left-1) {$p_s$};
\node[right=12pt of m-3-6] (left-1) {$q_{m_1}$};
\node[right=12pt of m-4-6] (left-1) {$p_{m_1}$};
\node[right=12pt of m-5-6] (left-1) {$q_{m_2}$};
\node[right=12pt of m-6-6] (left-1) {$p_{m_2}$};
\end{tikzpicture}
\end{equation}
\end{document}

在此处输入图片描述

答案2

这真的只是为了好玩,这就是为什么这是一个社区维基。在这种情况下,它并没有带来什么好处,但总的来说,这可能会带来好处。(对我上面的答案的一个可以想象的改进是,基线可以说更好。我不想更新我上面的答案,因为一些更有经验的用户可以猜到原因。)这个答案是关于的可能性

  1. 用样式绘制分割线;
  2. 以编程方式填充矩阵。

如果您拥有更大的矩阵,这可能会有回报。

\documentclass[11pt,twoside,a4paper]{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing, calc, positioning}
\tikzset{my matrix dividers/.style={execute at end matrix={
\pgfmathtruncatemacro{\Xmax}{\the\pgfmatrixcurrentcolumn/2-1}
\pgfmathtruncatemacro{\Ymax}{\the\pgfmatrixcurrentrow/2-1}
\foreach \XX in {1,...,\Xmax}
{\draw ($(\tikzmatrixname-1-\the\numexpr2*\XX\relax.north east)!0.5!
(\tikzmatrixname-1-\the\numexpr2*\XX+1\relax.north west)$) coordinate(aux) --
    ($(\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\numexpr2*\XX\relax.south east)!0.5!
(\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\numexpr2*\XX+1\relax.south west)$);}
\foreach \YY in {1,...,\Ymax}
{\draw ($(\tikzmatrixname-\the\numexpr2*\YY\relax-1.south west)!0.5!
(\tikzmatrixname-\the\numexpr2*\YY+1\relax-1.north west)$) coordinate(aux) --
    ($(\tikzmatrixname-\the\numexpr2*\YY\relax-\the\pgfmatrixcurrentcolumn.south east)!0.5!
(\tikzmatrixname-\the\numexpr2*\YY+1\relax-\the\pgfmatrixcurrentcolumn.north east)$);}
}}}
\begin{document}
\begin{equation}  
 Q=\begin{tikzpicture}[baseline=(X.base)]
\matrix [matrix of nodes,nodes in empty cells,left delimiter=(,right delimiter=),row
sep=0.5cm,column sep=0.5cm,
cells={execute at begin node={%
\ifnum\the\pgfmatrixcurrentcolumn<5
$\the\pgfmatrixcurrentcolumn$%
\else
$\the\numexpr\pgfmatrixcurrentcolumn-2$%
\fi
}},my matrix dividers] (m) {
&&&&& \\
&&&&& \\
&&&&& \\
&&&&& \\
&&&&& \\
&&&&& \\};
\node (X) at (m) {\phantom{X}};


\begin{scope}[nodes={text width=1.1em,align=center}]
 \node[above=10pt of m-1-1] (top-1) {$q_s$};
 \node[above=10pt of m-1-2] (top-2) {$p_s$};
 \node[above=10pt of m-1-3] (top-3) {$q_{m_1}$};
 \node[above=10pt of m-1-4] (top-4) {$p_{m_1}$};
 \node[above=10pt of m-1-5] (top-5) {$q_{m_2}$};
 \node[above=10pt of m-1-6] (top-6) {$p_{m_2}$};
\end{scope}

\coordinate[above=3pt of top-1] (aux);
\begin{scope}[nodes={text depth=0.25ex}]
 \draw[thick,decorate,decoration=brace] (top-1.west|-aux) -- (top-2.east|-aux)
  node[midway,above=0.1ex] {System};
 \draw[thick,decorate,decoration=brace] (top-3.west|-aux) -- (top-4.east|-aux)
  node[midway,above=0.1ex] {Meter 1};
 \draw[thick,decorate,decoration=brace] (top-5.west|-aux) -- (top-6.east|-aux)
  node[midway,above=0.1ex] {Meter 2};
\end{scope}


\node[right=12pt of m-1-6] (left-1) {$q_s$};
\node[right=12pt of m-2-6] (left-1) {$p_s$};
\node[right=12pt of m-3-6] (left-1) {$q_{m_1}$};
\node[right=12pt of m-4-6] (left-1) {$p_{m_1}$};
\node[right=12pt of m-5-6] (left-1) {$q_{m_2}$};
\node[right=12pt of m-6-6] (left-1) {$p_{m_2}$};
\end{tikzpicture}
\end{equation}
\end{document}

在此处输入图片描述

答案3

好的,现在我明白了。您想将矩阵上方的括号放置在更靠近系统文本等的位置,并确保这些括号不会相互重叠。

您遇到的问题是放置在节点内的矩形:

\node[rectangle,draw,fill= blue!30,opacity=.5,text opacity=1,above delimiter=\{]
 (del-top-1) at ($0.5*(top-1.south) +0.5*(top-2.south)$)
  {\tikz{\path (top-1.south west) rectangle (top-2.north east);}};% This rectangle creates the problem

这会在该屏幕截图中产生一个非常大的蓝色矩形:

屏幕截图

使用fit库来做同样的事情要容易得多。

\node[rectangle,above delimiter=\{,fit=(top-1) (top-2)] (del-top-1) {};
\node[above=10pt] at (del-top-1.north) {System};

为了使括号更小,只需放置inner sep=0pt选项即可。

\node[above=10pt of m-1-1,inner sep=0pt] (top-1) {$q_s$};
\node[above=10pt of m-1-2,inner sep=0pt] (top-2) {$p_s$};
\node[above=10pt of m-1-3,inner sep=0pt] (top-3) {$q_{m_1}$};
\node[above=10pt of m-1-4,inner sep=0pt] (top-4) {$p_{m_1}$};
\node[above=10pt of m-1-5,inner sep=0pt] (top-5) {$q_{m_2}$};
\node[above=10pt of m-1-6,inner sep=0pt] (top-6) {$p_{m_2}$};

通过使用该label选项,您可以避免命名节点del-top-1, etc并解决对齐问题anchor=mid

然后我们得到:

截屏

\documentclass[11pt,twoside,a4paper]{article}
\usepackage{braket,amsmath,amssymb}
\usepackage{tikz}
 \usetikzlibrary{matrix,decorations.pathreplacing, calc, positioning,fit}

\begin{document}
\begin{equation}  
 Q=\begin{tikzpicture}[baseline=(m-3-1.base) ]
\matrix [matrix of math nodes,left delimiter=(,right delimiter=),row sep=0.5cm,column sep=0.5cm
] (m) {
1&2&3&4&3&4 \\
1&2&3&4&3&4 \\
1&2&3&4&3&4 \\
1&2&3&4&3&4 \\
1&2&3&4&3&4 \\
1&2&3&4&3&4 \\};

\draw[solid] ($0.5*(m-1-2.north east)+0.5*(m-1-3.north west)$) --
     ($0.5*(m-6-3.south east)+0.5*(m-6-2.south west)$);
\draw[solid] ($0.5*(m-1-4.north east)+0.5*(m-1-5.north west)$) --
     ($0.5*(m-6-5.south east)+0.5*(m-6-4.south west)$);

\draw[solid] ($0.5*(m-2-1.south west)+0.5*(m-3-1.north west)$) --
 ($0.5*(m-2-6.south east)+0.5*(m-3-6.north east)$);

\draw[solid] ($0.5*(m-4-1.south west)+0.5*(m-5-1.north west)$) --
 ($0.5*(m-4-6.south east)+0.5*(m-5-6.north east)$);

\node[above=10pt of m-1-1,inner sep=0pt] (top-1) {$q_s$};
\node[above=10pt of m-1-2,inner sep=0pt] (top-2) {$p_s$};
\node[above=10pt of m-1-3,inner sep=0pt] (top-3) {$q_{m_1}$};
\node[above=10pt of m-1-4,inner sep=0pt] (top-4) {$p_{m_1}$};
\node[above=10pt of m-1-5,inner sep=0pt] (top-5) {$q_{m_2}$};
\node[above=10pt of m-1-6,inner sep=0pt] (top-6) {$p_{m_2}$};


%---------------- Use of the label option ---------------%
\node[rectangle,inner sep=0pt,above delimiter=\{,fit=(top-1) (top-2),label={[anchor=mid,yshift=15pt]:System}] {};

\node[rectangle,inner sep=0pt,above delimiter=\{,fit=(top-3) (top-4),label={[anchor=mid,yshift=15pt]:Meter 1}] {};

\node[rectangle,inner sep=0pt,above delimiter=\{,fit=(top-5) (top-6),label={[anchor=mid,yshift=15pt]:Meter 2}] {};
%---------------------------------------------------------

\node[right=12pt of m-1-6] (left-1) {$q_s$};
\node[right=12pt of m-2-6] (left-1) {$p_s$};
\node[right=12pt of m-3-6] (left-1) {$q_{m_1}$};
\node[right=12pt of m-4-6] (left-1) {$p_{m_1}$};
\node[right=12pt of m-5-6] (left-1) {$q_{m_2}$};
\node[right=12pt of m-6-6] (left-1) {$p_{m_2}$};

\end{tikzpicture}
\end{equation}

\end{document}

相关内容