Tikz 的 Latex 分段单位图表

Tikz 的 Latex 分段单位图表

需要用Tikz实现一个分段单位图表,基本代码如下:

\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage{polyglossia}
\setmainlanguage{spanish}
\usepackage{xcolor}
\usepackage{etoolbox}
\usepackage{float} %Paquete para posicionar Tablas
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, math}
\usetikzlibrary{calc}
\usetikzlibrary{tikzmark}
\usetikzlibrary{positioning}
\usetikzlibrary{decorations.pathreplacing,calligraphy}

\usepackage{musicography}% added for \musFlat{}
\usepackage{amsmath}%Para signo igual
\usepackage{musixtex} 
\input {musixfll}%
\input {musixper}%percusión
\input{musixlyr}% added for \lyrraise
\begin{document}%

\begin{tikzpicture}[node distance={38mm}, main/.style = {draw, rectangle},scale=0.6, every node/.style={scale=0.6}] 
\node[main] (1) {{\color{red}\verb|\chapter{}|}};
\node[] (1A) at ($(pic cs:1)+(0.0cm,0.6cm)$) {$1$};
\node[main] (2) [right of=1] {{\color{red}\verb|\section{}|}}; 
\node[] (2A) at ($(pic cs:2)+(0.0cm,0.6cm)$) {$1.1$}; 
\node[main] (3) [right of=2] {{\color{red}\verb|\subsection{}|}}; 
\node[] (3A) at ($(pic cs:3)+(0.0cm,0.6cm)$) {$1.1.1$}; 
\node[main] (4) [right of=3] {{\color{red}\verb|\subsubsection{}|}}; 
\node[] (4A) at ($(pic cs:3)+(0.0cm,0.6cm)$) {$1.1.1.1$}; 
\node[main] (5) [right of=4] {{\color{red}\verb|\paragraph{}|}}; 
\node[] (5A) at ($(pic cs:3)+(0.0cm,0.6cm)$) {$1.1.1.1.1$}; 
\node[main] (6) [right of=5] {{\color{red}\verb|\subparagraph{}|}}; 
\node[] (6A) at ($(pic cs:3)+(0.0cm,0.6cm)$) {$1.1.1.1.1.1$}; 
% Arrows
\draw[->] (1) -- (2);
\draw[->] (2) -- (3);
\draw[->] (3) -- (4);
\draw[->] (4) -- (5);
\draw[->] (5) -- (6);
% Braces
\draw [pen colour={orange},decorate, decoration = {calligraphic brace}] ($(pic cs:1)+(-1.3cm,1.1cm)$) --  ($(pic cs:6)+(5.3cm,1.1cm)$) node[pos=0.5,left=10pt,black]{\verb|book/report|};
\draw [pen colour={orange},decorate, decoration = {calligraphic brace,mirror}] ($(pic cs:2)+(1.3cm,-1.1cm)$) --  ($(pic cs:6)+(5.3cm,-1.1cm)$) node[pos=0.5,left=10pt,black]{\verb|article|};

\end{tikzpicture} 

\end{document}

当前输出如下: 当前的

期望的输出如下: 期望

答案1

您也可以使用几个嵌套\foreach循环和一些条件\infum语句。这是因为您的图表具有非常重复的结构。

例如:

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{calc,decorations.pathreplacing}

\begin{document}
\begin{tikzpicture}
\node (0) at (0,0) {};
\foreach[count=\j]\i in {chapter,section,subsection,subsubsection,paragraph,subparagraph}
{
  \pgfmathtruncatemacro\jj{\j-1}
  \node[right,draw,text=red] (\j) at ($(\jj.east)+(0.5,0)$) {\strut$\backslash$\texttt{\i}\{\}};
  \node[above] at (\j.north) {\foreach\k in {1,...,\j}{1\ifnum\k<\j.\fi}};
  \ifnum\j>1
    \node[below] at (\j.south) {\foreach\k in {1,...,\jj}{1\ifnum\k<\jj.\fi}};
    \draw[thick,->] (\jj) -- (\j);
  \fi
}
\draw [decorate,decoration={brace,amplitude=5mm,raise=5mm},thick,text=red]
      (1.north west) -- (6.north east) node [midway,yshift=1.2cm] {Book / Report};
\draw [decorate,decoration={brace,mirror,amplitude=5mm,raise=5mm},thick,text=red]
      (2.south west) -- (6.south east) node [midway,yshift=-1.2cm] {Article};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容