Tikz,子图和标题

Tikz,子图和标题

我尝试使用 tikz 并排绘制一些结,然后给它们贴上标签,但它无法与我的代码配合使用。我使用包:caption、subcaption、tikz(结)。

\usepackage[utf8]{inputenc}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{tikz}
\usetikzlibrary{matrix, arrows, knots}
\usepackage{graphicx}

\begin{document}
  \begin{figure}
    \begin{subfigure}[b]{0.45\textwidth}
      \centering
        \resizebox{\linewidth}{!}{           
          \begin{tikzpicture}
            \foreach \brk in {0,1,2} {
            \begin{scope}[rotate=\brk * 120]
            \node[knot crossing, transform shape,
            inner sep=1.5pt] (k\brk) at (0,-1) {};
            \end{scope}
            }
            \foreach \brk in {0,1,2} {
            \pgfmathparse{int(Mod(\brk - 1,3))}
            \edef\brl{\pgfmathresult}
            \draw[thick,red] (k\brk) .. controls (
            k\brk.4 north west) and (k\brl.4 north east) .. (k\brl.center);
            \draw[thick,red] (k\brk.center) .. controls (k\brk.16 south west) and (k\brl.16 south east) .. (k\brl);
            }
        \end{tikzpicture}
      }
    \caption{Trefoil}
    \label{a}
   \end{subfigure}
   \begin{subfigure}[b]{0.45\textwidth}
      \centering
        \resizebox{\linewidth}{!}{ 
          \begin{tikzpicture}
            \def\foil{3}
            \foreach \brk in {1,...,\foil} {
            \begin{scope}[rotate=\brk * 360/\foil]
            \node[transform shape, knot crossing, inner sep=1.5pt] (k\brk) at (0,-1) {};
            \end{scope}
            }
            \draw[thick,red] (0,0) \foreach \brk in {1,...,\foil} {let \na=\brk, \nb={int(Mod(\brk,\foil)+1)}, \nc={int(Mod(\brk+1,\foil)+1)} in (k\na) .. controls (k\na.16 south east) and (k\nb.16 south west) .. (k\nb.center) .. controls (k\nb.4 north east) and (k\nc.4 north west) .. (k\nc)};
          \end{tikzpicture}
        }
        \caption{Cinquefoil}
        \label{b}
      \end{subfigure}
    \end{figure}
  \caption{Examples of knots} 
  \end{figure}
\end{document}

答案1

正如评论中提到的,您的代码有几个错误:没有文档类、额外的\end{figure}、滥用letPGF 语法以及未能包含calc所需的库。(您包括了几件事不是需要编译您的示例,但省略了几个需要的。)

话虽如此,一旦这些错误被修复,这两个数字看起来大小似乎也差不多。事实上,它们看起来一模一样。

我最好的猜测是,尽管出现了错误,并且出现了可预见的不良结果,您仍会继续编译。即使是警告也不应该被忽略。错误需要修复。TeX 会告诉您事情出了问题,它不知道该怎么做。通常,如果您坚持,它会尝试做某事,但这并不是为了产生预期的输出。它的目的是可能为调试目的提供有用的信息。

结

\documentclass{article}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{tikz}
\usetikzlibrary{knots,calc}
\begin{document}
\begin{figure}
  \begin{subfigure}[b]{0.45\textwidth}
    \centering
    \resizebox{\linewidth}{!}{
      \begin{tikzpicture}
        \foreach \brk in {0,1,2} {
          \begin{scope}[rotate=\brk * 120]
            \node[knot crossing, transform shape,
            inner sep=1.5pt] (k\brk) at (0,-1) {};
          \end{scope}
        }
        \foreach \brk in {0,1,2} {
          \pgfmathparse{int(Mod(\brk - 1,3))}
          \edef\brl{\pgfmathresult}
          \draw[thick,red] (k\brk) .. controls (
          k\brk.4 north west) and (k\brl.4 north east) .. (k\brl.center);
          \draw[thick,red] (k\brk.center) .. controls (k\brk.16 south west) and (k\brl.16 south east) .. (k\brl);
        }
      \end{tikzpicture}
    }
    \caption{Trefoil}
    \label{a}
  \end{subfigure}
  \begin{subfigure}[b]{0.45\textwidth}
    \centering
    \resizebox{\linewidth}{!}{
      \begin{tikzpicture}
        \def\foil{3}
        \foreach \brk in {1,...,\foil} {
          \begin{scope}[rotate=\brk * 360/\foil]
            \node[transform shape, knot crossing, inner sep=1.5pt] (k\brk) at (0,-1) {};
          \end{scope}
        }
        \draw[thick,red] (0,0) \foreach \brk in {1,...,\foil} {let \n0=\brk, \n1={int(Mod(\brk,\foil)+1)}, \n2={int(Mod(\brk+1,\foil)+1)} in (k\n0) .. controls (k\n0.16 south east) and (k\n1.16 south west) .. (k\n1.center) .. controls (k\n1.4 north east) and (k\n2.4 north west) .. (k\n2)};
      \end{tikzpicture}
    }
    \caption{Cinquefoil}
    \label{b}
  \end{subfigure}
\caption{Examples of knots}
\end{figure}
\end{document}

相关内容