
私は現在、@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}
出力は次のようになります。
私は TikZ に慣れていませんが、次のことを理解しています。
- の効果は、
tikzmark
呼び出された正確な場所にノードを配置し、このノードの座標を後で使用できるようにすることです。 - これらの座標の使用を許可する
remember picture, overlay
オプション 。tikzpicture
ただし、 a tcolorbox
(および a tcbtheorem
) は TikZ を使用して描画されるため、これらの座標は失われてしまいます (ただし、問題は x 座標のみに由来し、y 座標は問題ないようです)。
tikzmark
は、作成するノードに名前 ( の値) を与えるようですmycaution
が、 の呼び出し\tikzmark
とボックスの描画の間に TikZ が使用されていないと仮定すると、この名前は実際には後で使用されません。ただし、この名前を使用して、ボックスがこのノードの右の余白にあるように指定する方法がわかりません...
誰か解決策(または、ここで実際に何が起こるのかの説明)を提供してもらえますか?
答え1
問題は、パッケージが定義するページ ノードの位置を内部的に計算するために使用されるtcolorbox
変更です。\textwidth
tikzpagenodes
これを確認するには
\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}
結果: