我设计了一些小命令来为我的笔记添加一个漂亮的“标注”;请考虑以下 MWE(该问题也在那里解释):
\documentclass{book}
\usepackage{tikz}
\tikzset{remember picture,
round mark/.style={
circle, draw=yellow, minimum size=5mm, fill opacity=0.5, fill=yellow,
},
}
\usepackage{pgfplots}\pgfplotsset{compat=1.9}
\usepackage{siunitx}
\DeclareSIUnit{\krypt}{\ensuremath{\mathcal{K}r}}
\usepackage{xparse}
\newcommand{\roundmark}[1]{\tikz[overlay, remember picture, baseline=-0.5ex]
\node [round mark, anchor=west] (#1) {};}
\NewDocumentCommand \addline {O{} m m }{%
\begin{tikzpicture}[overlay,remember picture]
\draw[ultra thick, yellow, ->, #1 ] (expl@#2) -- (#3);
\end{tikzpicture}%
}
\NewDocumentCommand \margincallout {O{} m +m }{%
\marginpar{
\begin{tikzpicture}[overlay,remember picture]
\node [ anchor=west, draw=orange, fill=yellow!20,
text width=\marginparwidth, align=left, font=\small,
#1 ] (expl@#2)
at (0,0) {
#3
};
\draw[ultra thick, yellow,->] (expl@#2) -- (#2);
\end{tikzpicture}}%
}
\begin{document}
I can mark thing in \roundmark{a}the main text and then have nice callouts from the marginpar to this point.
\margincallout{a}{This is a callout, a nice one and it's ok.}
% thanks @bloodworks http://tex.stackexchange.com/a/62285/38080
% require package capt-of
\begin{figure}[htb]
\begin{center}
\begin{tikzpicture}[remember picture,
]
\begin{axis}[
width=7cm,
xmin=0, xmax=10, domain=0:10,
enlarge y limits = 0.2,
ylabel = {Sensor R (\si{k\ohm})},
xlabel = {Kryporad (\si{\krypt})},
legend pos = north west,
legend style={nodes=right},
]
\addplot[red] {134 + 18.5*x + 15.0*sin(deg(0.664*x))};
\addplot[blue, dashed] {143 + 16.8*x};
\legend{non-linear fit, linear fit}
\end{axis}
\node [round mark] (hor-axis) at (4,-0.5) {};
\node [round mark] (ver-axis) at (-0.5,4) {};
%\draw (0,0) grid (5,5);
\end{tikzpicture}
\end{center}
\caption{With a bit of guesswork I can mark things in the graphics, too}
\label{fig:calibR}
\end{figure}
\margincallout[yshift=3cm]{hor-axis}{\textbf{Always} name the axis and columns, and use units when needed}
\addline{hor-axis}{ver-axis}
Uncomment the following to see the error:
%But if I want to put the callout \textbf{before} the mark,
%as in \margincallout{future}{A forward callout} it will not work, and give me an error.
%The mark will be \roundmark{future}here, for example.
\end{document}
输出一个好东西(IMHO):
问题是,有时候使用这个\margincallout
命令很有用前定义\roundmark
节点名称。
无论如何,该文档都需要两次传递;我在想也许可以写一些类似于标签的前向引用的东西......但我找不到任何解决方案。
是否可以引用下一次传递中要解决的节点?
更新:
所以我尝试了tikzmark
,但这里肯定有什么地方不对劲。看:
\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\tikzset{remember picture,
round mark/.style={
circle, draw=yellow, minimum size=5mm, fill opacity=0.5, fill=yellow,
},
}
\usepackage{pgfplots}\pgfplotsset{compat=1.9}
\usepackage{siunitx}
\DeclareSIUnit{\krypt}{\ensuremath{\mathcal{K}r}}
\usepackage{xparse}
\newcommand{\rtikzmark}[1]{\tikzmark[{
\node [baseline=-0.5ex, round mark, anchor=west]{}}]{#1}}
\NewDocumentCommand \addline {O{} m m }{%
\begin{tikzpicture}[overlay,remember picture]
\draw[ultra thick, yellow, ->, #1 ] (expl@#2) -- (pic cs:#3);
\end{tikzpicture}%
}
\NewDocumentCommand \margincallout {O{} m +m }{%
\marginpar{
\begin{tikzpicture}[overlay,remember picture]
\node [ anchor=west, draw=orange, fill=yellow!20,
text width=\marginparwidth, align=left, font=\small,
#1 ] (expl@#2)
at (0,0) {
#3
};
\draw[ultra thick, yellow,->] (expl@#2) -- (pic cs: #2);
\end{tikzpicture}}%
}
\begin{document}
I can mark thing in \rtikzmark{a}the main text and then have nice callouts from the marginpar to this point.
\margincallout{a}{This is a callout, a nice one and it's ok.}
% thanks @bloodworks http://tex.stackexchange.com/a/62285/38080
% require package capt-of
\begin{figure}[htb]
\begin{center}
\begin{tikzpicture}[remember picture,
]
\begin{axis}[
width=7cm,
xmin=0, xmax=10, domain=0:10,
enlarge y limits = 0.2,
ylabel = {Sensor R (\si{k\ohm})},
xlabel = {Kryporad (\si{\krypt})},
legend pos = north west,
legend style={nodes=right},
]
\addplot[red] {134 + 18.5*x + 15.0*sin(deg(0.664*x))};
\addplot[blue, dashed] {143 + 16.8*x};
\legend{non-linear fit, linear fit}
\end{axis}
\node [round mark] at (4,-0.5) {}; \tikzmark{hor-axis};
\node [round mark] at (-0.5,4) {}; \tikzmark{ver-axis};
%\draw (0,0) grid (5,5);
\end{tikzpicture}
\end{center}
\caption{With a bit of guesswork I can mark things in the graphics, too}
\label{fig:calibR}
\end{figure}
\margincallout[yshift=3cm]{hor-axis}{\textbf{Always} name the axis and columns, and use units when needed}
\addline{hor-axis}{ver-axis}
Uncomment the following to see the error:
But if I want to put the callout \textbf{before} the mark,
as in \margincallout{future}{A forward callout} it will not work, and give me an error.
The mark will be \rtikzmark{future}here, for example.
\end{document}
第一次调用(文本内)有点奏效,但线条没有画出来。第二次和第三次调用(标记点的调用)进入图形)将整个图形叠加变换......
好消息是前向调用确实有效,但是没有绘制线条。困惑。
答案1
(我补充一下多于因为它取代了原来的答案,所以有人只寻找什么而不关心如何,什么时候,当然不是为什么不应该费心阅读原始答案。)
《停止报》:tikzmark
再次进化
tikzmark
自首次推出以来,经历了相当大的发展,主要是由这个网站上的问题推动的。它在此过程中获得的一个关键特性是能够使用其保存的坐标事先的它在文档中出现。
它从一开始就具有的一个特性是,它旨在被使用外部命令\tikz
或tikzpicture
环境。在内部,我们的想法是,这样就可以完全访问 TikZ 的正常坐标记忆服务,因此不需要\tikzmark
。
然而,上述关键特征是不是TikZ 提供的功能。没有办法tikzpicture
在声明之前引用内部坐标。因此,如果想要这样做,就需要一些新东西。
这个问题的关键特征是,提问者希望使用相同的机制来引用 a 内部tikzpicture
和外部的坐标。因此,这表明在 tikzpictures 中提供这种透视坐标功能的正确方法是扩展 tikzmark。
这其实很简单。简而言之,tikzmark 所做的就是将标签与 tikzpicture 的原点关联起来,所有繁重的工作都在坐标系中完成。扩展是添加一个潜在的偏移量,这样一旦 tikz 确定了页面上所述 tikzpicture 的原点,它就会应用偏移量。将其与测试结合起来,看看我们是否在 tikzpicture 内,一切就完成了。
(值得注意的是,如果您切换到这个新版本,您应该在重新编译之前删除 aux 文件,否则您会在第一次重新编译时遇到很多错误。)
语法里面tikzpicture 是\tikzmark{label}{coordinate}
,例如\tikmark{a}{(3,4)}
。这意味着坐标(pic cs:a)
将指向(3,4)
当前图片中的点。
您可以从以下位置下载此新版本的 tikzmarkgithub:下载tikzmark.dtx
并运行tex tikzmark.dtx
以生成文件。
因此新的,改进的 \tikzmark
现在,代码再次恢复到合理状态:
\documentclass{book}
%\url{http://tex.stackexchange.com/q/295903/86}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\tikzset{remember picture,
round mark/.style={
circle, draw=yellow, minimum size=5mm, fill opacity=0.5, fill=yellow,
},
}
\usepackage{pgfplots}\pgfplotsset{compat=1.9}
\usepackage{siunitx}
\DeclareSIUnit{\krypt}{\ensuremath{\mathcal{K}r}}
\usepackage{xparse}
\newcommand{\rtikzmark}[1]{\tikzmark[{
\node [baseline=-0.5ex, round mark, anchor=west]{}}]{#1}}
\NewDocumentCommand \addline {O{} m m }{%
\begin{tikzpicture}[overlay,remember picture]
\draw[ultra thick, yellow, ->, #1 ] (expl@#2) -- (pic cs:#3);
\end{tikzpicture}%
}
\NewDocumentCommand \margincallout {O{} m +m }{%
\marginpar{
\begin{tikzpicture}[overlay,remember picture]
\node [ anchor=west, draw=orange, fill=yellow!20,
text width=\marginparwidth, align=left, font=\small,
#1 ] (expl@#2)
at (0,0) {
#3
};
\draw[ultra thick, yellow,->] (expl@#2) -- (pic cs:#2);
\end{tikzpicture}}%
}
\begin{document}
I can mark thing in \rtikzmark{a}the main text and then have nice callouts from the marginpar to this point.
\margincallout{a}{This is a callout, a nice one and it's ok.}
% thanks @bloodworks http://tex.stackexchange.com/a/62285/38080
% require package capt-of
\begin{figure}[htb]
\begin{center}
\begin{tikzpicture}[remember picture,
]
\begin{axis}[
width=7cm,
xmin=0, xmax=10, domain=0:10,
enlarge y limits = 0.2,
ylabel = {Sensor R (\si{k\ohm})},
xlabel = {Kryporad (\si{\krypt})},
legend pos = north west,
legend style={nodes=right},
]
\addplot[red] {134 + 18.5*x + 15.0*sin(deg(0.664*x))};
\addplot[blue, dashed] {143 + 16.8*x};
\legend{non-linear fit, linear fit}
\end{axis}
\node [round mark] at (4,-0.5) (hor-axis) {};
\node [round mark] at (-0.5,4) (ver-axis) {};
\node [round mark] at (0,0) (origin) {};
\tikzmark{hor-axis}{(hor-axis)}
\tikzmark{ver-axis}{(ver-axis)}
\tikzmark{origin}{(origin)}
%\draw (0,0) grid (5,5);
\end{tikzpicture}
\end{center}
\caption{With a bit of guesswork I can mark things in the graphics, too}
\label{fig:calibR}
\end{figure}
\margincallout[yshift=4cm]{hor-axis}{\textbf{Always} name the axis and columns, and use units when needed}
\addline{hor-axis}{ver-axis}
\addline{hor-axis}{origin}
Uncomment the following to see the error:
But if I want to put the callout \textbf{before} the mark,
as in \margincallout{future}{A forward callout} it will not work, and give me an error.
The mark will be \rtikzmark{future}here, for example.
\end{document}
(与下图相同)
原始答案如下:
这里有几件事……
(首先,我发现了tikzmark
与 交互中的一个错误marginpar
。我怀疑有错误的版本比 CTAN 上的版本新,因为你不应该得到任何事物在你的第二个例子中,如果你使用的是有缺陷的版本。无论如何,更新版本是可在 github 上获取。
其次,更重要的是,\tikzmark
它不是设计用来里面环境tikzpicture
。如果你这样做,事情就会出错。这甚至比使用正则表达式解析 HTML 更严重。关键是,如果你在环境中,tikzpicture
那么你可以访问 tikz 的所有节点和坐标定位内容,而不需要 tikzmark。
(请注意,无论我说了多少次“不要在 tikzpicture 中使用 tikzmark”,人们似乎都想使用它。也许我应该咬紧牙关,弄清楚如何让它发挥作用。)
诚然,这意味着您需要两个版本的标注代码:一个用于标记在 tikzpicture 中的情况,另一个用于标记不在 tikzpicture 中的情况。可能有巧妙的方法来编写代码以避免这种情况,但我脑子不太好,现在还不能想得那么远。
因此,尽管这永远不会在风格竞赛中获胜,但这是您的代码的工作版本,我认为它应该是正确的结果。
\documentclass{book}
%\url{http://tex.stackexchange.com/q/295903/86}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\tikzset{remember picture,
round mark/.style={
circle, draw=yellow, minimum size=5mm, fill opacity=0.5, fill=yellow,
},
}
\usepackage{pgfplots}\pgfplotsset{compat=1.9}
\usepackage{siunitx}
\DeclareSIUnit{\krypt}{\ensuremath{\mathcal{K}r}}
\usepackage{xparse}
\newcommand{\rtikzmark}[1]{\tikzmark[{
\node [baseline=-0.5ex, round mark, anchor=west]{}}]{#1}}
\NewDocumentCommand \addline {O{} m m }{%
\begin{tikzpicture}[overlay,remember picture]
\draw[ultra thick, yellow, ->, #1 ] (expl@#2) -- (pic cs:#3);
\end{tikzpicture}%
}
\NewDocumentCommand \addlineb {O{} m m }{%
\begin{tikzpicture}[overlay,remember picture]
\draw[ultra thick, yellow, ->, #1 ] (expl@#2) -- (#3);
\end{tikzpicture}%
}
\NewDocumentCommand \margincallout {O{} m +m }{%
\marginpar{
\begin{tikzpicture}[overlay,remember picture]
\node [ anchor=west, draw=orange, fill=yellow!20,
text width=\marginparwidth, align=left, font=\small,
#1 ] (expl@#2)
at (0,0) {
#3
};
\draw[ultra thick, yellow,->] (expl@#2) -- (pic cs:#2);
\end{tikzpicture}}%
}
\NewDocumentCommand \margincalloutb {O{} m +m }{%
\marginpar{
\begin{tikzpicture}[overlay,remember picture]
\node [ anchor=west, draw=orange, fill=yellow!20,
text width=\marginparwidth, align=left, font=\small,
#1 ] (expl@#2)
at (0,0) {
#3
};
\draw[ultra thick, yellow,->] (expl@#2) -- (#2);
\end{tikzpicture}}%
}
\begin{document}
I can mark thing in \rtikzmark{a}the main text and then have nice callouts from the marginpar to this point.
\margincallout{a}{This is a callout, a nice one and it's ok.}
% thanks @bloodworks http://tex.stackexchange.com/a/62285/38080
% require package capt-of
\begin{figure}[htb]
\begin{center}
\begin{tikzpicture}[remember picture,
]
\begin{axis}[
width=7cm,
xmin=0, xmax=10, domain=0:10,
enlarge y limits = 0.2,
ylabel = {Sensor R (\si{k\ohm})},
xlabel = {Kryporad (\si{\krypt})},
legend pos = north west,
legend style={nodes=right},
]
\addplot[red] {134 + 18.5*x + 15.0*sin(deg(0.664*x))};
\addplot[blue, dashed] {143 + 16.8*x};
\legend{non-linear fit, linear fit}
\end{axis}
\node [round mark] at (4,-0.5) (hor-axis) {};
\node [round mark] at (-0.5,4) (ver-axis) {};
\node [round mark] at (0,0) (origin) {};
%\draw (0,0) grid (5,5);
\end{tikzpicture}
\end{center}
\caption{With a bit of guesswork I can mark things in the graphics, too}
\label{fig:calibR}
\end{figure}
\margincalloutb[yshift=4cm]{hor-axis}{\textbf{Always} name the axis and columns, and use units when needed}
\addlineb{hor-axis}{ver-axis}
\addlineb{hor-axis}{origin}
Uncomment the following to see the error:
But if I want to put the callout \textbf{before} the mark,
as in \margincallout{future}{A forward callout} it will not work, and give me an error.
The mark will be \rtikzmark{future}here, for example.
\end{document}