tufte-book: キャプション内の tikz 参照

tufte-book: キャプション内の tikz 参照

documentclass を使用するとarticle、次のコードは期待どおりに動作します。

\documentclass{article}
\usepackage{lipsum}
\usepackage{caption}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usepgfplotslibrary{external}
% fix problems when tikzexternal is used
\newcommand{\tikzcaption}[1]{\protect\tikzset{external/export next=false}#1}
\newcommand{\tikzref}[1]{\tikzcaption{\ref{#1}}}

\tikzset{pointille/.style={dash pattern = on 2pt off 2pt on 6pt off 2pt}}
\tikzset{points/.style={dash pattern = on 1pt off 1pt}}
\tikzset{tirets/.style={dash pattern = on 5pt off 5pt}}

\begin{document}
\begin{figure}[!ht]
\centering
\captionsetup{width=9.5cm}
\pgfplotsset{every axis plot post/.append style={mark=none,line width=1.5pt}}
\begin{tikzpicture}
\begin{axis}
\addplot[tirets,color=blue]{2*x};\label{p4}
\addplot[pointille,color=green]{0.5*x*x};\label{p5}
\addplot[points,color=red]{-0.125*x*x*x};\label{p6}
\end{axis}
\end{tikzpicture}
\caption{This is a plot about colored curves: $f(x)=2 x$ (\tikzref{p4}),  $f(x)=0.5 x^2$ (\tikzref{p5}), and $f(x)=-0.125 x^3$ (\tikzref{p6})}
\end{figure}
\end{document}

ここに画像の説明を入力してください

ただし、\documentclass{tufte-book}参照を使用すると機能しません。

ここに画像の説明を入力してください

これをどうすれば修正できますか?

答え1

問題は、が LaTeX カーネルと同じ意味を持つという仮定の下でpgfplotsを操作していることですが、残念ながら ではこれは誤りです。\labeltufte-book

\documentclass{tufte-book}
\usepackage{lipsum}
\usepackage{pgfplots}
\usepackage{etoolbox}

% patch pgfplots so that \label does the original job
% tufte-book saves the original meaning of \label in
% \@tufte@orig@label
\makeatletter
\patchcmd{\pgfplots@environment@opt}{\label}{\@tufte@orig@label}{}{}
\makeatother

\pgfplotsset{compat=1.13}
\usepgfplotslibrary{external}
% fix problems when tikzexternal is used
\DeclareRobustCommand{\tikzcaption}[1]{\tikzset{external/export next=false}#1}
\DeclareRobustCommand{\tikzref}[1]{\tikzcaption{\ref{#1}}}

\tikzset{pointille/.style={dash pattern = on 2pt off 2pt on 6pt off 2pt}}
\tikzset{points/.style={dash pattern = on 1pt off 1pt}}
\tikzset{tirets/.style={dash pattern = on 5pt off 5pt}}

\begin{document}

\begin{figure}[!ht]
\centering
\pgfplotsset{every axis plot post/.append style={mark=none,line width=1.5pt}}
\begin{tikzpicture}
\begin{axis}
\addplot[tirets,color=blue]{2*x};\label{p4}
\addplot[pointille,color=green]{0.5*x*x};\label{p5}
\addplot[points,color=red]{-0.125*x*x*x};\label{p6}
\end{axis}
\end{tikzpicture}

\caption{This is a plot about colored curves: $f(x)=2 x$ (\tikzref{p4}), 
  $f(x)=0.5 x^2$ (\tikzref{p5}), and $f(x)=-0.125 x^3$ (\tikzref{p6})}

\end{figure}
\end{document}

私も作ってみまし\tikzrefたが\tikzcaption頑丈です。

ここに画像の説明を入力してください

答え2

質問に基づいてコードを作成していると思いますここpgfplots マニュアルのコードを使用して、よりクリーンな MWE を作成しようとしました。

ご覧のように、解決策はないしかし、もう少し先に進みました。3 つの\refコマンドのうち 2 つは期待どおりに機能しました。この場合、線とマークがあると機能しませんでした。

おそらくこれは、他の人たちが問題をより深く調べるのに役立つでしょう。

\documentclass{tufte-book}
\usepackage{lipsum}
\usepackage{caption}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}

\begin{document}

\chapter{Without a \texttt{figure} Environment}

% Taken from the pgfplots manuel section 4.9.6 (Legends with \label and \ref)

\begin{tikzpicture}
    \begin{axis}
        \addplot+[only marks,
            samples=15,
            error bars/y dir=both,
            error bars/y fixed=2.5]
            {3*x+2.5*rand};
        \label{pgfplots:label1}
        \addplot+[mark=none] {3*x};
        \label{pgfplots:label2}
        \addplot {4*cos(deg(x))};
        \label{pgfplots:label3}
    \end{axis}
\end{tikzpicture}

Label 1: \ref{pgfplots:label1}; Label 2: \ref{pgfplots:label2}; Label 3: \ref{pgfplots:label3}

\chapter{With a \texttt{figure} Environment}

% Taken from the pgfplots manuel section 4.9.6 (Legends with \label and \ref)

\begin{figure}
\centering
\begin{tikzpicture}
    \begin{axis}
        \addplot+[only marks,
            samples=15,
            error bars/y dir=both,
            error bars/y fixed=2.5]
            {3*x+2.5*rand};
        \label{pgfplots:label1}
        \addplot+[mark=none] {3*x};
        \label{pgfplots:label2}
        \addplot {4*cos(deg(x))};
        \label{pgfplots:label3}
    \end{axis}
\end{tikzpicture}
% Caption
\caption{Label 1: \ref{pgfplots:label1}; Label 2: \ref{pgfplots:label2}; Label 3: \ref{pgfplots:label3}}
\end{figure}

Label 1: \ref{pgfplots:label1}; Label 2: \ref{pgfplots:label2}; Label 3: \ref{pgfplots:label3}

\end{document}

ここに画像の説明を入力してください ここに画像の説明を入力してください

答え3

上記の解決策はうまくいきませんでした。理由はよくわかりませんが、上記の問題に対する貧乏人の解決策を思いつきました。これが誰かの役に立つかもしれません。

非常に単純なプロットしかないため、最終的にはキャプション テキストに色付きの em ダッシュを使用することにしました。

\caption{Ref to a plot \textcolor{blue}{---}}

まったく同じではありませんが、単純な状況では、これが最善の方法かもしれません。

関連情報