如何使 tikzpicture 矩阵居中

如何使 tikzpicture 矩阵居中

这是代码

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing, calc, positioning}
\begin{document}
\begin{center}
\begin{tikzpicture}
\matrix [matrix of math nodes,left delimiter=(,right delimiter=)] (m) {
    a_1&b_{1,2}&0&\cdots&0&b_{1,s_1+1}&0&0&\cdots&0&b_{2,s_2+1} \\
    b_{1,2}&a_2&b_{1,3} \\
    &\ddots&\ddots&\ddots \\
    &&b_{1,s_1-1}&a_{s_1-1}&b_{1,5} \\
    &&&b_{1,s_1}&a_{s_1}\\
    b_{1,s_1+1}&&&&0&a_{s_1+1}&b_{2,s_1+2} \\
    &&&&&b_{2,s_2+2}&a_{s_1+2}&b_{2,s_1+3}\\
    &&&&&&\ddots&\ddots&\ddots \\
    &&&&&&&b_{1,s_1-1}&a_{s_1-1}&b_{1,s_3} \\
    &&&&&&&&b_{1,5}&a_{s_2-1}   \\
    b_{2,s_2+1}&&&&&&&&&0&a_{s_3-1}&b_{1,s_3+1}\\       
    &&&&&&&&&&&&2&a_{s_3-1}&b_{1,s_3+1}\\               
    &&&&&&&&&&\ddots&\ddots&\ddots\\
    &&&&&&&&&&&1&2&3\\
};  
\node[above=10pt of m-1-1] (top-1){};
\node[above=10pt of m-1-3] (top-3){};
\node[above=10pt of m-1-4] (top-4){};
\node[above=10pt of m-1-5] (top-5){};   
\node[above=10pt of m-1-6] (top-6){};



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

\end{center}
\end{document}

这就是结果

在此处输入图片描述

矩阵向右浮动,其余部分不可见。如何将其居中?

答案1

您的矩阵已居中,但不幸的是它比可用的文本宽度更宽。最好的方法是重新构造它以适应可用的文本宽度。如果这不可能,您可以调整边距以适应矩阵:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing, calc, positioning}

\usepackage{changepage} 

\begin{document}
\begin{adjustwidth}{-7.6em}{-7.6em}
\begin{tikzpicture}
\matrix [matrix of math nodes,left delimiter=(,right delimiter=)] (m) {
    a_1&b_{1,2}&0&\cdots&0&b_{1,s_1+1}&0&0&\cdots&0&b_{2,s_2+1} \\
    b_{1,2}&a_2&b_{1,3} \\
    &\ddots&\ddots&\ddots \\
    &&b_{1,s_1-1}&a_{s_1-1}&b_{1,5} \\
    &&&b_{1,s_1}&a_{s_1}\\
    b_{1,s_1+1}&&&&0&a_{s_1+1}&b_{2,s_1+2} \\
    &&&&&b_{2,s_2+2}&a_{s_1+2}&b_{2,s_1+3}\\
    &&&&&&\ddots&\ddots&\ddots \\
    &&&&&&&b_{1,s_1-1}&a_{s_1-1}&b_{1,s_3} \\
    &&&&&&&&b_{1,5}&a_{s_2-1}   \\
    b_{2,s_2+1}&&&&&&&&&0&a_{s_3-1}&b_{1,s_3+1}\\       
    &&&&&&&&&&&&2&a_{s_3-1}&b_{1,s_3+1}\\               
    &&&&&&&&&&\ddots&\ddots&\ddots\\
    &&&&&&&&&&&1&2&3\\
};  
\node[above=10pt of m-1-1] (top-1){};
\node[above=10pt of m-1-3] (top-3){};
\node[above=10pt of m-1-4] (top-4){};
\node[above=10pt of m-1-5] (top-5){};   
\node[above=10pt of m-1-6] (top-6){};
\node[rectangle,above delimiter=\{] (del-top-3) at ($0.5*(top-3.south) +0.5*(top-5.south)$) {\tikz{\path (top-3.south west) rectangle (top-5.north east);}};
\node[above=5pt] at (del-top-3.north) {$N_1-1$};
\end{tikzpicture}
\end{adjustwidth}
\end{document}

相关内容