如何防止一系列 tikz 节点重叠?

如何防止一系列 tikz 节点重叠?

note对于我正在处理的命令,我有以下 MWE 。

\documentclass{article}
\usepackage[a5paper,marginparwidth=15mm]{geometry}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc}
\newcommand\note[2][0pt]{
  \tikz[overlay,remember picture,baseline=-1ex] \coordinate (e);
  \begin{tikzpicture}[remember picture, overlay]
  \path let \p1 = (e), \p2 = (current page marginpar area.west) in node[yshift=-#1,text width=\marginparwidth,align=left,anchor=north west,inner ysep=0pt,inner xsep=0pt,font=\normalfont\tiny\color{red}] at (\x2,\y1) {#2};
  \end{tikzpicture}
}
\usepackage{everypage}
\AddEverypageHook{
    \tikz[remember picture,overlay] {\draw [blue] (current page text area.south west) rectangle (current page text area.north east);}
    \tikz[remember picture,overlay] {\draw [green] (current page marginpar area.south west) rectangle (current page marginpar area.north east);}
}
\begin{document}
\section{\note{Note A}A Test Section}
Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.
Donec \note{Note B - pretty long one} vehicula augue eu neque. Pellentesque habitant morbi tristique senectus
et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra
metus rhoncus sem. \note{Note C} Nulla et lectus vestibulum urna fringilla \note{Note D} ultrices. Phasellus
eu tellus sit \note{Note E} amet tortor gravida placerat. Integer sapien est, iaculis in, pretium
quis, viverra ac, nunc.tus a mi. Morbi ac orci et nisl hendrerit mollis.
\end{document}

我希望Note E将 移至 下方Note D。目前我可以通过精确 手动完成此操作yshift,但我认为这可以轻松完成,无需手动调整。

我可以想到一个算法

  1. 保存音符底部的位置
  2. 放置下一个音符时,检查顶部位置是否小于前一个底部位置
  3. 计算所需yshift

但是作为初学者,我不知道如何在 中实现该行为tikz。任何帮助表示感谢:)

更新

这里还有一个次要问题 - 如果我尝试删除可选参数(只是为了做到这一点\newcommand\note[1]{)我最终会得到错误

! Use of \use@tikzlibrary doesn't match its definition.
\pgfutil@ifnextchar ...1\def \pgfutil@reserved@a { #2}\def \pgfutil@reserved@...
l.20 \section{\note{Note A}A Test Section}

我当然记得更改参考资料。发生了什么事?

测试代码

展示发生了align什么tabu

\documentclass{article}
\usepackage[a5paper,marginparwidth=15mm]{geometry}
\usepackage{tikzpagenodes}
\tikzset{
  marginpar node/.style={
    text width=\marginparwidth,
    align=left,
    inner sep=+0pt,
    font=\normalfont\tiny,
    text=red}}
\makeatletter
\def\tikz@marginpar@node@page{-1}%
\protected\def\note#1{%
  \tikzpicture[remember picture, overlay]
    \pgfutil@ifundefined{pgf@sh@ns@tikz@marginpar@node}{%
      \coordinate (@tikz@marginpar@node@c)at (0pt, 1ex-|current page marginpar area.west);
    }{%
      \ifnum\tikz@marginpar@node@page=\c@page\relax
        \pgfpointdiff{\pgfpointanchor{tikz@marginpar@node}{south west}}
                     {\pgfpoint{+0pt}{+1ex}}
        \ifdim\pgf@y<-3pt\relax
          \coordinate (@tikz@marginpar@node@c)
            at (0pt,1ex-|current page marginpar area.west);
        \else
          \coordinate (@tikz@marginpar@node@c)
            at ([yshift=-3pt]tikz@[email protected] west);
        \fi
      \else
        \coordinate (@tikz@marginpar@node@c)
            at (0pt,1ex-|current page marginpar area.west);
      \fi
    }
    \node [marginpar node/.try, anchor=north west, alias=tikz@marginpar@node]
            at (@tikz@marginpar@node@c) {\setlength\baselineskip{0pt}#1};
    \xdef\tikz@marginpar@node@page{\the\c@page}%
  \endtikzpicture}
\makeatother
\usepackage{everypage}
\AddEverypageHook{%
  \tikz[remember picture,overlay]{
    \draw[blue]  (current page text area.south west) rectangle
                    (current page text area.north east);
    \draw[green] (current page marginpar area.south west) rectangle
                    (current page marginpar area.north east);}%
}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhf[HL]{\def\note##1{}\leftmark}
\renewcommand{\sectionmark}[1]{\markboth{\thesection\ #1}{}}
\usepackage{tabu}
\usepackage{multicol}
\usepackage{amsmath}
\begin{document}
{\def\note#1{}\tableofcontents}

\section{\note{Note A}A Test Section}
Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.
Donec \note{Note B - pretty long one} vehicula augue eu neque.
Pellentesque habitant morbi tristique senectus
et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra
metus rhoncus sem. \note{Note C} Nulla et lectus vestibulum urna fringilla
\note{Note D} ultrices. Phasellus eu tellus sit \note{Note E} amet tortor
gravida placerat. Integer sapien est, iaculis in, pretium quis, viverra ac,
nunc.tus a mi. Morbi ac orci et nisl hendrerit mollis.

\newpage
\section{\note{Note A}A Test Section}
Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.
Donec \note{Note B - pretty long one} vehicula augue eu neque.
Pellentesque habitant morbi tristique senectus
et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra
metus rhoncus sem. \note{Note C} Nulla et lectus vestibulum urna fringilla
\note{Note D} ultrices. Phasellus eu tellus sit \note{Note E} amet tortor
gravida placerat. \note{Note F} Integer sapien est, iaculis in, pretium quis, 
viverra ac, nunc.tus a mi. Morbi ac orci et nisl hendrerit mollis.

\newpage
Morbi dolor\note{More testing} nulla, malesuada eu, pulvinar at, mollis ac, nulla. 
Cur-abitur auctor semper nulla. Donec varius orci eget risus. 
\begin{center}
Duis nibh mi, congue \note{center}
eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim
rutrum.
\end{center}
\begin{displaymath}
a+b+c =\note{display math} d+e+f
\end{displaymath}
Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi
auctor lorem non justo. Nam lacus libero, pretium at, \note{Test} lobortis vitae, 
ultricies et, tellus. Donec aliquet, tortor sed accumsan bibendum, erat ligula 
aliquet magna, vitae ornare odio metus a mi. Morbi ac orci et nisl hendrerit mollis.
\begin{multicols}{2}
Erat ligula aliquet magna,
vitae ornare odio metus a mi. Morbi nisl \note{Something is wrong here} mollis. 
Donec aliquet, tortor sed accumsan bibendum. Suspendisse
ut massa. Cras nec ante. Pellentesque a nulla. Cum sociis natoque penatibus et
magnis dis parturient montes, nascetur ridiculus mus. Aliquam tincidunt urna. 
Nulla ullamcorper vestibulum turpis. Nulla ullamcorper 
$f(x)=\note{This requires special attention}f(-x)$ vestibulum turpis. 
Pellentesque cursus luctus mauris.
\end{multicols}

Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi
auctor lorem non justo. Nam lacus libero, pretium at, lobortis vitae, 
ultricies et, tellus.

\begin{center}
\begin{tabular}{|c|c|c|c|c|}
\hline
Strop&Spag&H&Symbol&Nazwa\\\hline
0&4.05&\note{Table comment?}4.05&$Ps$&Piasek sredni\\\hline
4.05&5.30&1.25&$T$&Torf\\\hline
5.30&7.60&2.30&$Gz$&Glina zwiezla\\\hline
7.60&$\infty$&$\infty$&$P \pi$&Piasek pylasty\\\hline
\end{tabular}
\end{center}

\newpage
Morbi dolor\note{More testing} nulla, malesuada eu, pulvinar at, mollis ac, nulla. 
Cur-abitur auctor semper nulla. Donec varius orci eget risus. 
\begin{center}
Duis nibh mi, congue \note{center}
eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim
rutrum.
\end{center}
\begin{align}
a+b+c &= d+e+f \\
&\note{Rly equal?}= g +h \\
&= i.
\end{align}
Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi
auctor lorem non justo. Nam lacus libero, pretium at, \note{Test} 
lobortis vitae, ultricies et,
tellus. Donec aliquet, tortor sed accumsan bibendum, erat ligula aliquet magna,
vitae ornare odio metus a mi. Morbi ac orci et nisl hendrerit mollis.
\begin{multicols}{2}
Erat ligula aliquet magna, vitae ornare odio 
metus a mi. Morbi ac orci et nisl \note{Something is wrong here} mollis. 
Donec aliquet, tortor sed accumsan bibendum. Suspendisse
ut massa. Cras nec ante. Pellentesque a nulla. Cum sociis natoque penatibus et
magnis dis parturient montes, nascetur ridiculus mus. Aliquam tincidunt urna. 
Nulla ullamcorper vestibulum turpis. Nulla ullamcorper 
$f(x)=\note{This requires special attention}f(-x)$ vestibulum turpis. 
Pellentesque cursus luctus mauris.
\end{multicols}

Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi
auctor lorem non justo. Nam lacus libero, pretium at, lobortis vitae, 
ultricies et, tellus.

\begin{center}
\begin{tabu}{|c|c|c|c|c|}
\hline
Strop&Spag&H&Symbol&Nazwa\\\hline
0&4.05&\note{Table comment?}4.05&$Ps$&Piasek sredni\\\hline
4.05&5.30&1.25&$T$&Torf\\\hline
5.30&7.60&2.30&$Gz$&Glina zwiezla\\\hline
7.60&$\infty$&$\infty$&$P \pi$&Piasek pylasty\\\hline
\end{tabu}
\end{center}

\newpage
Restart?\note{restart}

\end{document}

答案1

无 - 解决方案calc

首先,我们检查是否存在节点。如果存在,则计算和tikz@marginpar@node之间的差值(除非前一个注释在不同的页面上)。如果(tikz@[email protected] west)(0pt, 1ex)小于 0 的部分(0pt, 1ex)太高,因此tikz@marginpar@node使用前一个来定位新节点。

代码

\documentclass{article}
\usepackage[a5paper,marginparwidth=15mm]{geometry}
\usepackage{tikzpagenodes}
\tikzset{
  marginpar node/.style={
    text width=\marginparwidth,
    align=left,
    inner sep=+0pt,
    font=\normalfont\tiny,
    text=red}}
\makeatletter
\def\tikz@marginpar@node@page{-1}%
\protected\def\note#1{%
  \tikzpicture[remember picture, overlay]
    \pgfutil@ifundefined{pgf@sh@ns@tikz@marginpar@node}{%
      \coordinate (@tikz@marginpar@node@c)at (0pt, 1ex-|current page marginpar area.west);
    }{%
      \ifnum\tikz@marginpar@node@page=\c@page\relax
        \pgfpointdiff{\pgfpointanchor{tikz@marginpar@node}{south west}}
                     {\pgfpoint{+0pt}{+1ex}}
        \ifdim\pgf@y<0pt\relax
          \coordinate (@tikz@marginpar@node@c)
                                          at (0pt,1ex-|current page marginpar area.west);
        \else
          \coordinate (@tikz@marginpar@node@c)
                                      at ([yshift=-.75ex]tikz@[email protected] west);
        \fi
      \else
        \coordinate (@tikz@marginpar@node@c)
                                          at (0pt,1ex-|current page marginpar area.west);
      \fi
    }
    \node [marginpar node/.try, anchor=north west, alias=tikz@marginpar@node]
                                                 at (@tikz@marginpar@node@c) {#1};
    \xdef\tikz@marginpar@node@page{\the\c@page}%
  \endtikzpicture}
\makeatother
\usepackage{everypage}
\AddEverypageHook{%
  \tikz[remember picture,overlay]{
    \draw[blue]  (current page text area.south west)      rectangle
                                                     (current page text area.north east);
    \draw[green] (current page marginpar area.south west) rectangle
                                             (current page marginpar area.north east);}%
}
\begin{document}
{\def\note#1{}\tableofcontents}

\section{\note{Note A}A Test Section}
Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.
Donec \note{Note B - pretty long one} vehicula augue eu neque.
Pellentesque habitant morbi tristique senectus
et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra
metus rhoncus sem. \note{Note C} Nulla et lectus vestibulum urna fringilla
\note{Note D} ultrices. Phasellus eu tellus sit \note{Note E} amet tortor
gravida placerat. Integer sapien est, iaculis in, pretium quis, viverra ac,
nunc.tus a mi. Morbi ac orci et nisl hendrerit mollis.

\newpage
\section{\note{Note A}A Test Section}
Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.
Donec \note{Note B - pretty long one} vehicula augue eu neque.
Pellentesque habitant morbi tristique senectus
et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra
metus rhoncus sem. \note{Note C} Nulla et lectus vestibulum urna fringilla
\note{Note D} ultrices. Phasellus eu tellus sit \note{Note E} amet tortor
gravida placerat. Integer sapien est, iaculis in, pretium quis, viverra ac,
nunc.tus a mi. Morbi ac orci et nisl hendrerit mollis.
\end{document}

输出

在此处输入图片描述

相关内容