尝试绘制下图时,如果安装了 hyperref 库,latex 会抛出错误。
知道为什么吗?
\documentclass[landscape]{article}
\usepackage[landscape,margin=1cm]{geometry}
\pagestyle{empty}
\usepackage{tikz}
\usetikzlibrary{positioning,backgrounds}
%**%CAUSES ERROR WHEN IMPORTED%**
\usepackage[hidelinks]{hyperref}
\begin{document}
\centering
\begin{tikzpicture}[x=13mm,y=9mm]
% some styles
\tikzset{
box/.style={
minimum height=5mm,
inner sep=.7mm,
outer sep=0mm,
text width=10mm,
text centered,
font=\small\bfseries\sffamily,
text=#1!50!black,
draw=#1,
line width=.25mm,
rotate=0,
},
link/.style={-latex,line width=.3mm},
plus/.style={,},
}
% Pascal's triangle
% row #0 => value is 1
\node (p-0-0) at (0,0) {1};
\foreach \row in {1,...,8} {
% col #0 => value is 1
\node (p-\row-0) at (-\row/2,-\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
% position of each value
\coordinate (pos) at (-\row/2+\col,-\row);
% odd color for odd value and even color for even value
\pgfmathtruncatemacro{\rest}{mod(\value,2)}
\ifnum \rest=0
\node (p-\row-\col) at (pos) {\value};
\else
\node (p-\row-\col) at (pos) {\value};
\fi
% for arrows and plus sign
\ifnum \col<\row
\node[plus,above=0mm of p-\row-\col]{+};
\pgfmathtruncatemacro{\prow}{\row-1}
\pgfmathtruncatemacro{\pcol}{\col-1}
\draw[link] (p-\prow-\pcol) -- (p-\row-\col);
\draw[link] ( p-\prow-\col) -- (p-\row-\col);
\fi
}
}
\begin{pgfonlayer}{background}
% filling and drawing with the same color to enlarge background
\path[draw=white,line width=5mm,rounded corners=2.5mm]
( p-0-0.north west) -- ( p-0-0.north east) --
(p-8-8.north east) -- (p-8-8.south east) --
( p-8-0.south west) -- ( p-8-0.north west) --
cycle;
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
答案1
重命名\value
为其他名称:
\documentclass[landscape]{article}
\usepackage[landscape,margin=1cm]{geometry}
\pagestyle{empty}
\usepackage{tikz}
\usetikzlibrary{positioning,backgrounds}
%**%CAUSES ERROR WHEN IMPORTED%**
\usepackage[hidelinks]{hyperref}
\begin{document}
\centering
\begin{tikzpicture}[x=13mm,y=9mm]
% some styles
\tikzset{
box/.style={
minimum height=5mm,
inner sep=.7mm,
outer sep=0mm,
text width=10mm,
text centered,
font=\small\bfseries\sffamily,
text=#1!50!black,
draw=#1,
line width=.25mm,
rotate=0,
},
link/.style={-latex,line width=.3mm},
plus/.style={,},
}
% Pascal's triangle
% row #0 => value is 1
\node (p-0-0) at (0,0) {1};
\foreach \row in {1,...,8} {
% col #0 => value is 1
\node (p-\row-0) at (-\row/2,-\row) {1};
\pgfmathsetmacro{\myvalue}{1};
\foreach \col in {1,...,\row} {
% iterative formula : val = precval * (row-col+1)/col
% (+ 0.5 to bypass rounding errors)
\pgfmathtruncatemacro{\myvalue}{\myvalue*((\row-\col+1)/\col)+0.5};
\global\let\myvalue=\myvalue
% position of each value
\coordinate (pos) at (-\row/2+\col,-\row);
% odd color for odd value and even color for even value
\pgfmathtruncatemacro{\rest}{mod(\myvalue,2)}
\ifnum \rest=0
\node (p-\row-\col) at (pos) {\myvalue};
\else
\node (p-\row-\col) at (pos) {\myvalue};
\fi
% for arrows and plus sign
\ifnum \col<\row
\node[plus,above=0mm of p-\row-\col]{+};
\pgfmathtruncatemacro{\prow}{\row-1}
\pgfmathtruncatemacro{\pcol}{\col-1}
\draw[link] (p-\prow-\pcol) -- (p-\row-\col);
\draw[link] ( p-\prow-\col) -- (p-\row-\col);
\fi
}
}
\begin{pgfonlayer}{background}
% filling and drawing with the same color to enlarge background
\path[draw=white,line width=5mm,rounded corners=2.5mm]
( p-0-0.north west) -- ( p-0-0.north east) --
(p-8-8.north east) -- (p-8-8.south east) --
( p-8-0.south west) -- ( p-8-0.north west) --
cycle;
\end{pgfonlayer}
\end{tikzpicture}
\end{document}