
我設計了幾個命令來為我的筆記添加一個漂亮的「標註」;請考慮以下 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}
它輸出了一個很好的(恕我直言)的東西:
問題是有時使用命令很有\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 內部的座標。因此,如果一個人想做到這一點,就需要一些新的東西。
這個問題的關鍵特徵是詢問者希望使用相同的機制來引用 a 內部tikzpicture
和外部的座標。因此,這表明在 tikzpictures 內提供這種透視座標功能的正確方法是擴展 tikzmark。
事實證明這很容易。簡而言之,tikzmark 所做的是將標籤與 tikzpicture 的原點關聯起來,所有繁重的工作都是在座標系中完成的。擴展是增加一個潛在的偏移量,這樣一旦 tikz 計算出所述 tikz 圖片的原點在頁面上的位置,它就會套用該偏移量。將其與測試結合起來,看看我們是否在 tikzpicture 內,一切就完成了。
(值得注意的是,如果你切換到這個新版本,你應該在重新編譯之前刪除 aux 文件,否則第一次重新編譯時會出現很多錯誤。)
文法裡面tikzpicture 是\tikzmark{label}{coordinate}
,例如\tikmark{a}{(3,4)}
。這意味著座標(pic cs:a)
將指向(3,4)
當前圖片中的點。
您可以從以下位置下載 tikzmark 的新版本github:下載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}