Tikz:其他節點邊距中的節點與 tcbtheorem 交互

Tikz:其他節點邊距中的節點與 tcbtheorem 交互

我目前正在嘗試使用@GonzaloMedina 的答案為邊注創建框架環境

考慮以下簡化程式碼:

\documentclass[oneside]{book}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{tikzpagenodes}
\usepackage{lipsum}


\newcounter{mycaution}
\newcommand\tikzmark[1]{%
  \tikz[remember picture,overlay]
  \node[inner xsep=0pt,outer sep=0pt] (#1) {};
}

\newcommand{\caution}{
\stepcounter{mycaution}
\tikzmark{\themycaution}%
\begin{tikzpicture}[remember picture,overlay]
\node[draw=red,anchor=west,xshift=\marginparsep,yshift=0pt]   
  (mybox\themycaution)
  at ([yshift=3pt]current page text area.east|-\themycaution) 
  {\parbox{\marginparwidth}{Some text}};
\end{tikzpicture}
}

\begin{document}


\lipsum[1]
Some random text\caution{}
\lipsum[2-5]
\end{document}

該命令\caution執行了我所期望的操作:它在右側邊距中產生一個紅色框,位於呼叫 之前的文字右側\caution

現在,假設我想做同樣的事情,但這\caution是在tcbtheorem環境內部調用的。例如:

\documentclass[oneside]{book}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{tikzpagenodes}
\usepackage[most]{tcolorbox}
\tcbuselibrary{theorems}
\usepackage{lipsum}


\newcounter{mycaution}
\newcommand\tikzmark[1]{%
  \tikz[remember picture,overlay]
  \node[inner xsep=0pt,outer sep=0pt] (#1) {};
}

\newcommand{\caution}{
\stepcounter{mycaution}
\tikzmark{\themycaution}%
\begin{tikzpicture}[remember picture,overlay]
\node[draw=red,anchor=west,xshift=\marginparsep,yshift=0pt]   
  (mybox\themycaution)
  at ([yshift=3pt]current page text area.east|-\themycaution) 
  {\parbox{\marginparwidth}{Some text}};
\end{tikzpicture}
}

\newtcbtheorem{theo}{Theorem}{theorem style=plain}{th}

\begin{document}
\begin{theo}{}{}

Some random text\caution{}
\lipsum[1]
\end{theo}

\lipsum[1]
Some random text\caution{}

\end{document}

然後輸出如下:

輸出-tcb

我不太習慣 TikZ,但我的理解是:

  • 的作用tikzmark是在呼叫的確切位置放置一個節點a,而這個節點的座標可以在後面使用,
  • 允許使用這些座標remember picture, overlay的選項 。tikzpicture

然而, a tcolorbox(因此 a tcbtheorem)是使用 TikZ 繪製的,因此這些座標有點丟失(除了問題僅來自 x 座標,y 座標似乎很好)。

似乎tikzmark給它創建的節點一個名稱( 的值mycaution),但是我們稍後並沒有真正使用這個名稱,假設在呼叫\tikzmark和繪製盒子之間沒有使用 TikZ 。但是,我不知道如何使用這個名稱來指定我的框應該位於該節點的邊緣...

任何人都可以提供解決方案(或更好:解釋這裡到底發生了什麼)?

答案1

問題在於,套件使用的tcolorbox變更在內部計算它定義的頁面節點的位置。\textwidthtikzpagenodes

你可以看到這個使用

\documentclass[oneside]{book}
\usepackage{tikzpagenodes}
\usepackage[most]{tcolorbox}

\newtcbtheorem{theo}{Theorem}{theorem style=plain}{th}

\begin{document}

\the\textwidth
\begin{theo}{}{}
\the\textwidth
\end{theo}

\end{document}

這使:

在此輸入影像描述

所以在 tcolorbox 內部,current page text area.east節點位於其實際位置的左側。

防止這種情況的一種方法是使用我們theo在包的幫助下在環境之前檢索的正確錨點etoolbox,並重新定義\caution以使用更正的錨點:

\documentclass[oneside]{book}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{tikzpagenodes}
\usepackage[most]{tcolorbox}
\tcbuselibrary{theorems}
\usepackage{lipsum}
\usepackage{etoolbox}
\usetikzlibrary{fit}

\newcounter{mycaution}
\newcommand\tikzmark[1]{%
  \tikz[remember picture,overlay]
  \node[inner xsep=0pt,outer sep=0pt] (#1) {};
}

\newcommand{\caution}{
\stepcounter{mycaution}%
\tikzmark{\themycaution}%
\begin{tikzpicture}[remember picture,overlay]
\node[draw=red,anchor=west,xshift=\marginparsep,yshift=0pt]   
  (mybox\themycaution)
  at ([yshift=3pt]current page text area.east|-\themycaution) 
  {\parbox{\marginparwidth}{Some text}};
\end{tikzpicture}
}

\newtcbtheorem{theo}{Theorem}{theorem style=plain}{th}

\BeforeBeginEnvironment{theo}{%
\tikz[remember picture,overlay]
  \node[fit=(current page text area),line width=0,inner sep=0,name=correct current page text area]{};
\renewcommand{\caution}{
\stepcounter{mycaution}%
\tikzmark{\themycaution}%
\begin{tikzpicture}[remember picture,overlay]
\node[draw=red,anchor=west,xshift=\marginparsep,yshift=0pt]   
  (mybox\themycaution)
  at ([yshift=3pt]correct current page text area.east|-\themycaution) 
  {\parbox{\marginparwidth}{Some text}};
\end{tikzpicture}%
}%
}

\begin{document}

\begin{theo}{}{}
Some random text\caution{}
\lipsum[1]
\end{theo}

\lipsum[1]
Some random text\caution{}

\end{document}

結果:

在此輸入影像描述

相關內容