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呼び出された正確な場所にノードを配置し、このノードの座標を後で使用できるようにすることです。
  • これらの座標の使用を許可する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これを防ぐ 1 つの方法は、パッケージの助けを借りて環境の前に取得する正しいアンカーを使用し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}

結果:

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

関連情報