在 pgfplot 的 ylabel 中使用 \ref

在 pgfplot 的 ylabel 中使用 \ref

當我使用\label\ref來區分兩個 y 軸時,繪製的標記在\ref與標籤的其餘部分一起旋轉後不再在線上(請參閱下面的範例)。有任何想法嗎?

\documentclass{article}

\usepackage{pgfplots}

\begin{document}
  \begin{tikzpicture}
    \begin{axis}[axis y line*=left, scale only axis,
                 ylabel={estimation 2 \ref{pgf:allsamples}}]
      \addplot[blue,mark=+] table {data2.csv};
      \label{pgf:allsamples}
    \end{axis}
    \begin{axis}[axis y line*=right, scale only axis,
             ylabel={estimation 1 \ref{pgf:swingvotes}}, ylabel near ticks]
      \addplot[red,mark=x] table {data1.csv};
      \label{pgf:swingvotes}
    \end{axis}
  \end{tikzpicture}
\end{document}

ref 沒有如預期般運作

答案1

事實上,pgfplots 中存在一個錯誤:該\ref命令檢查是否「它是從 tikz 圖片中調用的」。但這個檢查是有問題的,因為它,嗯從 tikzpicture 內調用,但 tikzpicture 已中斷以顯示節點。

由於該錯誤,標籤出現了意外的位移。

在錯誤修復之前,您可以使用以下解決方法:

\documentclass{article}

\usepackage{pgfplots}

\def\fixcheck{%
    \def\tikzifinpicture##1##2{##2}%
}%

\begin{document}
  \begin{tikzpicture}
    \begin{axis}[axis y line*=left, scale only axis,
                 ylabel={\fixcheck estimation 2 \ref{pgf:allsamples}}]
      \addplot[blue,mark=+] {x+5};
      \label{pgf:allsamples}
    \end{axis}
    \begin{axis}[axis y line*=right, scale only axis,
             ylabel={\fixcheck estimation 1 \ref{pgf:swingvotes}}, ylabel near ticks]
      \addplot[red,mark=x] {x};
      \label{pgf:swingvotes}
    \end{axis}
  \end{tikzpicture}
\end{document}

在所有標籤中,它會新增\fixcheck宏,這只會導致檢查回傳「false」。在這種情況下,\ref將會新增臨時 tikzpicture。

相關內容