编辑

编辑

我想要实现的是这幅画(请原谅我糟糕的绘画技巧): 在此处输入图片描述

这是我所知道的:

在此处输入图片描述

它基本上是由以下代码组成的数组:

\documentclass[border=5pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,cryptocode}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage[english]{babel}

\title{DS Assignment 2}
\author{that's me}
 \date{February 2018}

\setlength{\parindent}{0pt}

\newcommand\LinearGraph[1]{%
\begin{tikzpicture}[box/.style={rectangle,draw=gray, thick, minimum width=5mm}]
 \foreach \num/\lab [count=\c] in {#1} {
   \node[box,label=below:\lab] at (\c/2,0){$\num$};
 }
  \end{tikzpicture}%
}



\begin{document}

\maketitle

\textbf{Exercise 6)} \\some text

$ \phantom{x}\hspace{20ex} $ \LinearGraph{1/0, 2/1, 2/2, 3/3}

\end{document}

我怎样才能从已有的内容中得到我想要的东西?或者如果需要新代码,我不介意添加它。请注意,箭头并不是从每个单元格指向另一个单元格。我希望能够选择这一点,最好是单侧箭头,我不关心颜色。

答案1

我不太确定你想用你正在做的宏来处理这个问题。但是,只要你简单地命名节点,就可以根据需要向宏添加箭头。可以arrows.meta轻松选择箭头样式,包括半头箭头。以下是每种箭头各一个的示例。

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,bending}
\newcommand\LinearGraph[1]{%
  \begin{tikzpicture}[box/.style={rectangle,draw=gray, thick, minimum width=5mm}]
    \foreach \numb/\lab [count=\c] in {#1} {
      \node (n\c) [box,label=below:\lab] at (\c/2,0){$\numb$};
    }
    \draw [-{Stealth[left,bend]}] ([yshift=1.5pt]n1.south) [bend right=50] to ([yshift=1.5pt]n2.south);
    \draw [-{Stealth[bend]}] ([yshift=1.5pt]n2.south) [bend right=50] to ([yshift=1.5pt]n3.south);
    \draw [-{Stealth[right,bend]}] ([yshift=1.5pt]n3.south) [bend right=50] to ([yshift=1.5pt]n4.south);
  \end{tikzpicture}%
}
\begin{document}
\LinearGraph{1/0, 2/1, 2/2, 3/3}
\end{document}

箭头变化

编辑

对于更灵活的方法,我倾向于使用pic最直接的解决方案。例如,

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,bending}
\tikzset{%
  linear graph/.search also={/tika},
  linear graph/.pic={
    \tikzset{
      linear graph/.cd,
      #1,
      /tikz/.cd
    }
    \coordinate (bo) at (0,0);
    \foreach \numb/\lab [count=\c, remember=\c as \clast (initially o)] in \lineargraphboxes
    {
      \node (b\c) [box, label={[text height=1.25ex]below:\lab}, anchor=west] at (b\clast.east){$\numb$};
    }
    \foreach \i/\j in \lineargrapharrows \draw [-{Stealth[bend]}] ([yshift=1.5pt]b\i.south) [bend right=50] to ([yshift=1.5pt]b\j.south);
    \foreach \i/\j in \lineargraphleftarrows \draw [-{Stealth[bend,left]}] ([yshift=1.5pt]b\i.south) [bend right=50] to ([yshift=1.5pt]b\j.south);
    \foreach \i/\j in \lineargraphrightarrows \draw [-{Stealth[bend,right]}] ([yshift=1.5pt]b\i.south) [bend right=50] to ([yshift=1.5pt]b\j.south);
  },
  box/.style={
    draw=gray, thick, minimum width=5mm, text height=1.5ex,
  },
  linear graph/.cd,
  boxes/.estore in=\lineargraphboxes,
  boxes=,
  arrows/.estore in=\lineargrapharrows,
  arrows=,
  left arrows/.estore in=\lineargraphleftarrows,
  left arrows=,
  right arrows/.estore in=\lineargraphrightarrows,
  right arrows=,
}
\begin{document}
\begin{tikzpicture}
  \pic {linear graph = {boxes={1/0, 2/1, 2/2, 3/3}}};
  \pic at (0,-1)  {linear graph = {boxes={1/a, B/b, 20/c, 76.54/d, Aardvarks/e}, arrows={1/2,3/4}, left arrows={2/3}, right arrows={1/5,4/5}}};
\end{tikzpicture}
\end{document}

这意味着第一种情况将以无箭头的形式绘制,而第二种情况将在第一个和第二个以及第三个和第四个框之间有完整的箭头,第二个和第三个之间有左箭头,第一个和第五个以及第四个和第五个之间有右箭头。

变化:有箭头和无箭头

编辑 编辑

以下是我不推荐的做法,因为这会使你的代码更加晦涩难懂。但是,如果你真的想要一个宏,你可以添加

\newcommand\LinearGraph[1]{% not recommended!
  \tikz{\pic {linear graph = {#1}};}%
}

然后制作相同的纸带式图表

% not recommended!
\LinearGraph{boxes={1/0, 2/1, 2/2, 3/3}}
\LinearGraph{boxes={1/a, B/b, 20/c, 76.54/d, Aardvarks/e}, arrows={1/2,3/4}, left arrows={2/3}, right arrows={1/5,4/5}}

在您的文档中。

答案2

在此处输入图片描述

对于上述解决方案,我为新命令定义了两个参数:#1包含单元格数量并 #2包含单元格内容及其标签:

\documentclass{article}
\usepackage{tikz}

\newcommand\LinearGraph[2]{%
\begin{tikzpicture}[box/.style={rectangle,draw=gray, thick, minimum width=5mm}]
 \foreach \num/\lab [count=\c] in {#2}%
{
   \node (n\c) [box,label=below:\lab] at (\c/2,0){$\num$};
   \ifnum\c<#1
   \draw[shorten >=1pt, shorten <=1pt, ->, thick, red]
        (n\c.base) to [out=270, in=270, looseness=2] ++ (0.4,0);
   \fi
 }
  \end{tikzpicture}
  }

\begin{document}
\[
\LinearGraph{4}{1/0, 2/1, 2/2, 3/2}
\]

\[
\LinearGraph{5}{1/0, 2/1, 2/2, 3/2, 4/3}
\]
\end{document}

相关内容