hyperref 和 TikZ 之间奇怪的冲突

hyperref 和 TikZ 之间奇怪的冲突

下面是一个代码,在不加载 hyperref 的情况下可以运行,但在加载 hyperref 时(使用 LuaLaTeX 和 shell escape),即使是最后一个包,也无法编译。以下是输出的最后几行:

Package lastpage Warning: Rerun to get the references right on input line 60.
AED: lastpage setting LastPage
! Missing number, treated as zero.
<to be read again>
{
l.60 \end{document}

我不知道出了什么问题。我在这里读过很多关于处理与 hyperref 相关的困难的答案,但没有一个能解决问题。

\documentclass[11pt,svgnames, english, french]{article}

\RequirePackage{ifluatex}
\ifluatex
\RequirePackage{fontspec}
\RequirePackage{luacode}
\else
\RequirePackage[utf8]{inputenc}
\RequirePackage[T1]{fontenc}
\fi

\usepackage{babel}\frenchsetup{og=«, fg=»}

\usepackage{tikz}
\usetikzlibrary{backgrounds,calc,chains,fadings,fit,scopes,shapes,shadows,arrows,arrows.meta,positioning,bending,shadows.blur,shapes.misc,tikzmark,patterns,angles,quotes,spy,decorations.shapes}

\usepackage[most]{tcolorbox}
\tcbuselibrary{breakable}

\usepackage{array, amsmath, bm, colortbl, enumitem, esvect, etoolbox, fancyvrb, fontawesome, fourier-otf, graphicx, icomma, ifthen, marvosym, minted, multicol, multirow, relsize, tasks, textcomp, url, xcolor, xparse, xspace}
\usepackage[object=vectorian]{pgfornament} %%  http://altermundus.com/pages/tkz/ornament/index.html
\usepackage[autolanguage,np]{numprint}
\usepackage{lastpage}
%\usepackage[unicode]{hyperref} % <<<--- UNCOMMENT TO SEE BUG !
%\usepackage{cleveref}

\begin{document}

Bla

\begin{tikzpicture}
\tikzset{
    val/.style={circle, fill=black, text=white, text centered, font=\small\bfseries, text width=2.2ex, inner sep=.3ex, outer sep=0mm},
    link/.style={-{Latex[length=3mm,width=5mm]}, shorten >=1.5ex, line width=1.5ex} }
    \pgfmathsetmacro{\depth}{8}
    \node[val] (p-0-0) at (0,0) {1};
    \foreach \row in {1,...,\depth} {
        \node[val] (p-\row-0) at (0,-\row) {1};
        \pgfmathsetmacro{\value}{1};
        \foreach \col in {1,...,\row} {
            % iterative formula : val = precval * (row-col+1)/col (+ 0.5 to bypass rounding errors)
            \pgfmathtruncatemacro{\value}{\value*((\row-\col+1)/\col)+0.5};
            \global\let\value=\value
            \coordinate (pos) at (\col,-\row);
            \node[val] (p-\row-\col) at (pos) {} ;
            \pgfmathtruncatemacro{\prow}{\row-1}
            \pgfmathtruncatemacro{\pcol}{\col-1}
            \ifnum \col<\row
                \begin{scope}[transparency group, opacity=.4]
                    \pgfmathtruncatemacro{\opacity}{80 - 60 * abs(mod(\col-\row, 2))}
                    \draw[link, blue!\opacity!red] (p-\prow-\pcol.center) node[fill, white, circle, inner sep=1ex] {} -| node[fill, circle, inner sep=1ex] {} (p-\row-\col.center);
                \end{scope} ;
            \fi
            \pgfmathparse{\col<=\row && \row<\depth}
            \ifnum \pgfmathresult>0
                \node at ($(p-\row-\pcol)!0.5!(p-\row-\col)$) {+} ;
            \fi
            \node[val] at (pos) {\value} ; } }
\end{tikzpicture}
\end{document}

答案1

如果您将宏重命名\value为其他名称(例如\Value大写 V),问题似乎就解决了。

一般来说,使用已在基本 TeX 或 LaTeX 代码中使用的自定义宏名称不是一个好主意。该宏\value是这些基本宏之一,用于返回计数器的整数值。

除此之外,您还应该检查是否真的需要加载所有这些包。我简化了您的示例,以包含 MWE 真正需要的宏和库(当然仍然包括包hyperref)。

\documentclass[11pt]{article}

\usepackage{tikz}
\usetikzlibrary{calc,arrows.meta}

\usepackage[unicode]{hyperref} 

\begin{document}

Bla

\begin{tikzpicture}
\tikzset{
    val/.style={circle, fill=black, text=white, text centered, font=\small\bfseries, text width=2.2ex, inner sep=.3ex, outer sep=0mm},
    link/.style={-{Latex[length=3mm,width=5mm]}, shorten >=1.5ex, line width=1.5ex} }
    \pgfmathsetmacro{\depth}{8}
    \node[val] (p-0-0) at (0,0) {1};
    \foreach \row in {1,...,\depth} {
        \node[val] (p-\row-0) at (0,-\row) {1};
        \pgfmathsetmacro{\Value}{1};                      % <-- !
        \foreach \col in {1,...,\row} {
            % iterative formula : val = precval * (row-col+1)/col (+ 0.5 to bypass rounding errors)
            \pgfmathtruncatemacro{\Value}{\Value*((\row-\col+1)/\col)+0.5};
            \global\let\Value=\Value
            \coordinate (pos) at (\col,-\row);
            \node[val] (p-\row-\col) at (pos) {};
            \pgfmathtruncatemacro{\prow}{\row-1}
            \pgfmathtruncatemacro{\pcol}{\col-1}
            \ifnum \col<\row
                \begin{scope}[transparency group, opacity=.4]
                    \pgfmathtruncatemacro{\opacity}{80 - 60 * abs(mod(\col-\row, 2))}
                    \draw[link, blue!\opacity!red] (p-\prow-\pcol.center) node[fill, white, circle, inner sep=1ex] {} -| node[fill, circle, inner sep=1ex] {} (p-\row-\col.center);
                \end{scope} 
            \fi
            \pgfmathparse{\col<=\row && \row<\depth}
            \ifnum \pgfmathresult>0
                \node at ($(p-\row-\pcol)!0.5!(p-\row-\col)$) {+};
            \fi
            \node[val] at (pos) {\Value}; } }
\end{tikzpicture}
\end{document}

相关内容