创建三角表

创建三角表

有没有办法创建看起来像这样的东西? 在此处输入图片描述

我已经查看了\rotatebox命令,但是它不会在底部得到相同的“锯齿状”边缘,并且查看了如何用 TikZ 绘制对角分割网格?这似乎适用(尤其是与 结合使用\rotatebox)。

我可以做到这一点:

在此处输入图片描述

代码改编自链接,但我不知道如何定制它。

\documentclass[12pt]{article}
\usepackage[english]{babel}
\usepackage{amsfonts}
\usepackage{tikz}
\usepackage{rotating}
\usetikzlibrary{calc,decorations.shapes}
\tikzset{
  decorate with/.style args={#1 separated by #2}{
    fill,
    decorate,decoration={shape backgrounds,shape=#1,shape size=1.5mm,
    shape sep={#2, between borders}}
  }
}

\pgfkeys{/tikz/.cd,
   num quad/.initial=5,
   num quad/.get=\numquad,
   num quad/.store in=\numquad,
}

\begin{document}

\usetikzlibrary{calc,decorations.shapes}
\rotatebox{315}{\begin{tikzpicture}[x=0.5025cm,y=0.5025cm,line cap=round]
    \foreach \x [count=\xi] in {1,...,\numquad}{
      \foreach \y [count=\yi] in {\x,...,\numquad}{
        \node [draw, minimum size=0.5cm,outer sep=0pt,inner sep=0pt] (u-\xi\yi) at (\xi,-\yi) {};
      }
    }
    \end{tikzpicture}
}
\end{document}

答案1

diamond形状仅用于绘制和填充,文本实际上只是菱形节点的标签(实际上也是一个节点)。

只有在 45 度角的情况下,这种方法才最有效
。也可以使用自定义坐标系(X去 ↗,通过使用旋转 (↘) 而不是旋转,正方形/菱形也可以用矩形路径来绘制。

形状的大小手动设置为

minimum size=1.414cm+0.4\pgflinewidth

的系数\pgflinewidth是根据经验得出的,并且被选择为使得线条相互覆盖,就像grid那样。

更新:
remember循环结束后,不再记住 所使用的宏。我正为此使用它。(由于不应用相同的解析,因此仍然需要global remember密钥。)remember=\macroglobal rememberremember

加上于 →米 →我使用自定义/utils/exec={<cond>}{<true keys>}{<false keys>}键将标签添加到标签中。(我们也可以\ifnum在这里使用,或者我们也可以\ifnum/utils/ifs 替换 s。)

包装mathtools已装入以便\mathrlap轻松放置→标签。

代码

\documentclass[tikz]{standalone}
\usepackage{mathtools}
\usetikzlibrary{shapes.geometric}
\makeatletter
\pgfqkeys{/pgf/foreach}{
  global remember/.code=%
    \pgfutil@append@tomacro{\pgffor@remember@code}{\gdef\noexpand#1{#1}}}
\pgfqkeys{/utils}{if/.code n args={3}{%
  \pgfmathifthenelse{#1}{1}{0}\ifnum\pgfmathresult=0 
  \expandafter\pgfutil@secondoftwo\else\expandafter\pgfutil@firstoftwo\fi
  {\pgfkeysalso{#2}}{\pgfkeysalso{#3}}}}
\makeatother
\begin{document}
\begin{tikzpicture}[
    rotate=-45,
    every label/.append style={text depth=+0pt},
    label position=center,
    every cell/.style={fill=gray!25},
    column 3/.style={fill=red!25},
    row 5/.style={fill=green!25},
    cell 2-2/.style={fill=gray},
    cell 3-2/.style={fill=gray!50},
    ]
\foreach \jRow[count=\jCount from 1, remember=\jCount, global remember=\jCount] in {%
        0,%
        {15750,0},%
        {7875,2625,0},%
        {9375,4375,750,0},%
        {11875,7125,2500,1000,0},%
        {15125,10500,5375,3500,5000,0}%
    } {
    \foreach \mCell[count=\mCount from 1, remember=\mCount] in \jRow {
        \node[
            diamond,
            minimum size=1.414cm+0.4\pgflinewidth,
            draw,
            every cell/.try,
            row \jCount/.try,
            column \mCount/.try,
            cell \jCount-\mCount/.try,
            label={\pgfmathprintnumber{\mCell}},
            alias=@lastnode,
            alias=@lastrow-\mCount
        ] at (\mCount-.5,\jCount-.5) {};
        \ifnum\mCount=1
            \path [late options={name=@lastnode,
              label={
                [/utils/if={\jCount==1}%
                  {label={[rotate=45,anchor=south]above left:$j\mathrlap{{}\to}$}}{}]
                above left:$\jCount$}}];
        \fi
    }
        \path [late options={name=@lastnode, label=below:$A_\jCount$}];
    }
    \foreach \jCountExtra in {1,...,\jCount}
        \path [late options={name=@lastrow-\jCountExtra, label={
          [/utils/if={\jCountExtra==1}%
            {label={[rotate=-45,anchor=south]above right:$m\mathrlap{{}\to}$}}{}]
           above right:$\jCountExtra$}}];
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

答案2

TikZ 有一个rotate选项。以下是示例:

% without rotate
\begin{tikzpicture}[y={(0, -1)}]
    \path (0.5, -0.5) node{1} ++(1, 0) node{2};
    \path (-0.5, 0.5) node{1} ++(0, 1) node{2};
    \draw (0, 2) grid (2, 0);
    \node at (0.5, 0.5) {$12.34$};
    \node at (2.5, 2.5) {$A_1$};
\end{tikzpicture}

% with rotate
\begin{tikzpicture}[y={(0, -1)}, rotate=-45]
    \path (0.5, -0.5) node{1} ++(1, 0) node{2};
    \path (-0.5, 0.5) node{1} ++(0, 1) node{2};
    \draw (0, 2) grid (2, 0);
    \node at (0.5, 0.5) {$12.34$};
    \node at (2.5, 2.5) {$A_1$};
\end{tikzpicture}

结果如下:

在此处输入图片描述


更新:

\begin{tikzpicture}[y={(0, -1)}, rotate=-45]
    \path (0.5, -0.5) node{1} ++(1, 0) node{2};
    \path (-0.5, 0.5) node{1} ++(0, 1) node{2};
    \foreach \y in {0,...,2} {
        \foreach \x in {0,...,\y} {
            \draw (\x, \y - \x) rectangle +(1, 1);
        }
    }
    \node at (0.5, 0.5) {$12.34$};
    \node at (2.5, 2.5) {$A_1$};
\end{tikzpicture}

结果:

在此处输入图片描述

答案3

这是一个解决方案nicematrix。表格按经典表格构建。然后将其旋转 -45°。表格的每个单元格都旋转 45°,以便显示水平文本。

\documentclass{article}
\usepackage{nicematrix,tikz,collcell}

\begin{document}

\NiceMatrixOptions
  {
    code-for-first-col = \rotatebox{45}{\arabic{iRow}},
    code-for-first-row = \rotatebox{45}{\arabic{jCol} \rule[-5mm]{0mm}{5mm}}
  }

\newcolumntype{K}{>{\collectcell\Rotate}c<{\endcollectcell}}

\newcommand{\Rotate}[1]{\rotatebox[origin=c]{45}{\OnlyMainNiceMatrix{\rule[-10.6mm]{0mm}{21.2mm}\clap{#1}}}}

\rotatebox{-45}
{
\tabcolsep=0pt
\begin{NiceTabular}{KKKKKK}%
  [ hvlines,corners=SE,first-row,first-col,columns-width=15mm ]
\CodeBefore
  \arraycolor{gray!10}
  \cellcolor{gray!80}{5-2,2-3}
  \cellcolor{gray!60}{4-2,2-4}
  \cellcolor{gray!40}{3-2,2-5}
\Body
& 1 & 2 & 3 & 4 & 5 & 6 \\ 
& 15.125 & 10.500 & 5.375 & 3.500 & 5.000 & 0 \\
& 11.875 & 7.215 & 2.500 & 1.000 & 0 \\
& 9.375 & 4.375 & 750 & 0 \\
& 7.875 & 2.625 & 0 \\
& 15.750 & 0 \\
& 0 \\
\CodeAfter
  \begin{tikzpicture} [every node/.style = {below,rotate=45}]
  \node at (7-|2) {$A_1$} ; 
  \node at (6-|3) {$A_2$} ; 
  \node at (5-|4) {$A_3$} ; 
  \node at (4-|5) {$A_4$} ; 
  \node at (3-|6) {$A_5$} ; 
  \node at (2-|7) {$A_6$} ; 
\end{tikzpicture}
\end{NiceTabular}
}

\end{document}

您需要多次编译(因为nicematrix在后台使用 PGF/Tikz 节点)。

上述代码的输出

您还可以将说明\cellcolor放在主表格中,也就是说,放在相应的单元格中,就像使用 命令\cellcolor一样colortbl

\documentclass{article}
\usepackage{nicematrix,tikz,collcell}

\begin{document}

\NiceMatrixOptions
  {
    code-for-first-col = \rotatebox{45}{\arabic{iRow}},
    code-for-first-row = \rotatebox{45}{\arabic{jCol} \rule[-5mm]{0mm}{5mm}}
  }

\newcolumntype{K}{>{\collectcell\Rotate}c<{\endcollectcell}}

\newcommand{\Rotate}[1]{\rotatebox[origin=c]{45}{\OnlyMainNiceMatrix{\rule[-10.6mm]{0mm}{21.2mm}\clap{#1}}}}

\rotatebox{-45}
{
\tabcolsep=0pt
\begin{NiceTabular}{KKKKKK}%
  [ hvlines,corners=SE,first-row,first-col,columns-width=15mm, colortbl-like ]
\CodeBefore
  \arraycolor{gray!10}
\Body
& 1 & 2 & 3 & 4 & 5 & 6 \\ 
& 15.125 &  10.500 &  5.375 & 3.500 & 5.000 & 0 \\
& 11.875 & 7.215 & \cellcolor{gray!80} 2.500 & \cellcolor{gray!60} 1.000 & \cellcolor{gray!40}0 \\
& 9.375 & \cellcolor{gray!40} 4.375 & 750 & 0 \\
& 7.875 & \cellcolor{gray!60} 2.625 & 0 \\
& 15.750 & \cellcolor{gray!80} 0 \\
& 0 \\
\CodeAfter
  \begin{tikzpicture} [every node/.style = {below,rotate=45}]
  \node at (7-|2) {$A_1$} ; 
  \node at (6-|3) {$A_2$} ; 
  \node at (5-|4) {$A_3$} ; 
  \node at (4-|5) {$A_4$} ; 
  \node at (3-|6) {$A_5$} ; 
  \node at (2-|7) {$A_6$} ; 
\end{tikzpicture}
\end{NiceTabular}
}

\end{document}

输出是一样的。

相关内容