如何正确更改每个案例中 marginpar 出现的一侧?

如何正确更改每个案例中 marginpar 出现的一侧?

我正在尝试使用marginpartodonotes在 latex 文件中发表评论,以简化协作。页边距只是快速的待办事项注释和修复,我不想对每个页边距注释的位置进行硬编码,因为它们是临时的。因此,我尝试在序言中创建小脚本并在进行过程中使用它们。

我的问题是,对于长边距文本,一个段落/页面中的多个边距文本要么重叠,要么离实际文本太远。自然而然地,我认为我应该能够根据我的情况将边距更改为出现在页面的左侧或右侧。也就是说,我希望能够定义命令,比如说,\lmargin所有rmargin带有的边距注释都\lmargin出现在一侧,所有带有的边距注释\rmargin都出现在另一侧。我并不真正关心哪一个在左边,哪一个在右边,只要它们在相对的两侧即可。或者理想情况下,如果 latex 可以跟踪边距注释并自动在左右之间交替,那就太好了。

搜索论坛后,唯一的解决方案是更改整个文档的边距。我能找到的最接近的解决方案是使用\reversemarginpar。只要边距注释在不同的段落中,它就可以工作,但我不想只为文本中添加的每个注释而打破段落。

这是 marginpar 的 MnonWE,todonotes 包类似:

\documentclass[]{article}

\usepackage{pgf}
\usepackage[ %
            %top=Bcm, 
            %bottom=Hcm, 
            %outer=Ccm, 
            %inner=Acm, 
            %heightrounded, 
            marginparwidth=2.5cm, 
            %marginparsep=Ecm
            ]{geometry}
\usepackage{lipsum}
\usepackage{soul} % for highlighting the text: \hl
\usepackage{setspace} % for setting the linespace for a portion of the text: \setstretch

\definecolor{dkblue}{rgb}{0.0, 0.0, 0.6}
\definecolor{ltblue}{rgb}{0.6, 0.6, 1.0}

\usepackage{color}

\newcommand{\lmargin}[2]{\sethlcolor{ltblue}\hl{#1}\marginpar{\setstretch{0.5} {\color{dkblue} \tiny note: #2}}}


\begin{document}
And even there \lmargin{are}{there's something here that I need to pay attention to, and I need to be very clear about it, so I am going to write everything that comes to my mind here in the margin so that it doesn't take anyone 400 years to come up with the same thing!}  more things that can be said, but I am \reversemarginpar\lmargin{not sure}{Same thing here: there's something here that I need to pay attention to, and I need to be very clear about it, so I am going to write everything that comes to my mind here in the margin so that it doesn't take anyone 400 years to come up with the same thing!}  exactly what. \lipsum[7] 

Do you see the problem \reversemarginpar\lmargin{here}{And this is what I need}? If I don't go to a new paragraph, everything changes altogether! and overlaps even when I do!


\end{document}

它看起来是这样的: 在此处输入图片描述

答案1

当我看着我的旧 paracol 解决方案我开始想我可以做得更好。特别是我应该能够在每个段落中处理多个注释。

此解决方案的优点是它永远不会重叠,也不会跨页中断。由于注释正在写入 AUX 文件,因此我将大部分格式放入其中\marginparformat。此外,它需要运行两次。

实现此方法的主要问题是您只能在段落之间切换列,这可以使用 来解决\everypar。即便如此,您也需要在段落开头知道接下来会发生什么。解决方案是将所需信息写入 AUX 文件。当您切换回原始列时,它会立即启动另一列,\everypar您必须忽略它,否则就会陷入循环。

需要注意的是,\everypar往往不会被保留。我重写了\@afterheading,但肯定还会有其他的。

\documentclass{article}
\usepackage[ %
            %top=Bcm, 
            %bottom=Hcm, 
            %outer=Ccm, 
            %inner=Acm, 
            %heightrounded, 
            textwidth={\dimexpr 5in+5cm+2\columnsep},
            marginparwidth=0pt, 
            marginparsep=0pt
            ]{geometry}
\usepackage[nopar]{lipsum}
\usepackage{soul} % for highlighting the text: \hl
\usepackage{setspace} % for setting the linespace for a portion of the text: \setstretch

\usepackage{color}
\definecolor{dkblue}{rgb}{0.0, 0.0, 0.6}
\definecolor{ltblue}{rgb}{0.6, 0.6, 1.0}

\usepackage{paracol}
  \setcolumnwidth{2.5cm, 5in, 2.5cm}

\newcommand{\marginparformat}% only change between paragraphs
 {\tiny% affects em and ex
  \parindent=2em
  \parskip=1ex
  \sloppy
  \setstretch{0.5}%
  \color{dkblue}%
  \noindent note: }

\newcounter{absparagraph}
\newcounter{leftmarginpar}
\newcounter{rightmarginpar}
\globalcounter{absparagraph}
\globalcounter{leftmarginpar}
\globalcounter{rightmarginpar}


\makeatletter
\newcommand{\leftmarginpar}[1]% #1=text
{\stepcounter{leftmarginpar}%
 \pdfsavepos
 \protected@write\@auxout{}{\string\newleftmarginpar{\theleftmarginpar}{\theabsparagraph}%
   {\thepage}{\noexpand\number\pdflastypos}{#1}}}%

\newcommand{\rightmarginpar}[1]% #1=text
{\stepcounter{rightmarginpar}%
 \pdfsavepos
 \protected@write\@auxout{}{\string\newrightmarginpar{\therightmarginpar}{\theabsparagraph}%
   {\thepage}{\noexpand\number\pdflastypos}{#1}}}%

%begin gory details
\newcommand{\pagetop@y}{\dimexpr \paperheight-1in-\topmargin-\headheight-\headsep}
\newcommand{\current@page}{}% reserve global name

\newif\ifrepeatpar

\newlength{\leftmarginpar@y}
\newlength{\rightmarginpar@y}
\setlength{\leftmarginpar@y}{\pagetop@y}
\setlength{\rightmarginpar@y}{\pagetop@y}

\newcommand{\newleftmarginpar}[5]% #1 = index, #2 = paragraph, $3 = page, #4 = y location, #5 = text
{\expandafter\gdef\csname leftmarginpar@#2\endcsname{#1}%
 \expandafter\gdef\csname leftmarginpar@page@#1\endcsname{#3}%
 \expandafter\gdef\csname leftmarginpar@y@#1\endcsname{#4}%
 \expandafter\gdef\csname leftmarginpar@text@#1\endcsname{#5}}

\newcommand{\newrightmarginpar}[5]% #1 = index, #2 = page, #3 = y location, #4 = text
{\expandafter\gdef\csname rightmarginpar@#2\endcsname{#1}%
 \expandafter\gdef\csname rightmarginpar@page@#1\endcsname{#3}%
 \expandafter\gdef\csname rightmarginpar@y@#1\endcsname{#4}%
 \expandafter\gdef\csname rightmarginpar@text@#1\endcsname{#5}}

\newcommand{\AtBeginParagraph}{\everypar{}%
\ifrepeatpar\repeatparfalse\else
  \stepcounter{absparagraph}%
  \xdef\current@page{\arabic{page}}%
  \switchcolumn[0]
    \loop\ifnum\value{page}<\current@page\relax% sync pages
      \newpage
      \global\leftmarginpar@y=\pagetop@y\relax
    \repeat
    \@ifundefined{leftmarginpar@\theabsparagraph}{}{\bgroup
      \count1=\csname leftmarginpar@\theabsparagraph\endcsname\relax
      \count2=\value{leftmarginpar}%
      \loop\ifnum\count2<\count1
        \advance\count2 by 1
        \count3=\csname leftmarginpar@page@\number\count2\endcsname\relax% check page
        {\loop\ifnum\value{page}<\count3
          \newpage
          \global\leftmarginpar@y=\pagetop@y\relax
        \repeat}% sync page
        \dimen0=\csname leftmarginpar@y@\number\count2\endcsname sp\relax% compute offset
        \ifdim\dimen0<\leftmarginpar@y
          \dimen1=\dimexpr \leftmarginpar@y-\dimen0\relax
        \else
          \dimen1=\topskip
        \fi
        \setbox0=\vbox{\everypar{}%
          {\marginparformat
          \rule{0pt}{\dimen1}% I can't tell is fhis is adding space
          \csname leftmarginpar@text@\number\count2\endcsname
          \par}}%
        \global\advance\leftmarginpar@y by \dimexpr-\ht0-\dp0\relax
        \unvbox0%
      \repeat
    \egroup}%
  \switchcolumn[2]
    \loop\ifnum\value{page}<\current@page\relax% sync pages
      \newpage
      \global\rightmarginpar@y=\pagetop@y\relax
    \repeat
    \@ifundefined{rightmarginpar@\theabsparagraph}{}{\bgroup
      \count1=\csname rightmarginpar@\theabsparagraph\endcsname\relax
      \count2=\value{rightmarginpar}%
      \loop\ifnum\count2<\count1
        \advance\count2 by 1
        \count3=\csname rightmarginpar@page@\number\count2\endcsname\relax% check page
        {\loop\ifnum\value{page}<\count3
          \newpage
          \global\rightmarginpar@y=\pagetop@y\relax
        \repeat}% sync page
        \dimen0=\csname rightmarginpar@y@\number\count2\endcsname sp\relax% compute offset
        \ifdim\dimen0<\rightmarginpar@y
          \dimen1=\dimexpr \rightmarginpar@y-\dimen0\relax
        \else
          \dimen1=\topskip
        \fi
        \setbox0=\vbox{\everypar{}% write text
          {\marginparformat
          \rule{0pt}{\dimen1}%
          \csname rightmarginpar@text@\number\count2\endcsname
          \par}}%
        \global\advance\rightmarginpar@y by \dimexpr-\ht0-\dp0\relax
        \unvbox0%
      \repeat
    \egroup}%
  \switchcolumn[1]
  \everypar{\AtBeginParagraph}%
  \repeatpartrue
\fi}

\def\@afterheading{%
 \@nobreaktrue
 \everypar{%
   \if@nobreak
     \@nobreakfalse
     \clubpenalty \@M
     \if@afterindent \else
       {\setbox\z@\lastbox}%
     \fi
   \else
     \clubpenalty \@clubpenalty
   \everypar{\AtBeginParagraph}%
 \fi
 \AtBeginParagraph}}
\makeatother

\newcommand{\lmargin}[2]{\sethlcolor{ltblue}\hl{#1}\leftmarginpar{#2}}
\newcommand{\rmargin}[2]{\sethlcolor{ltblue}\hl{#1}\rightmarginpar{#2}}

\usepackage{blindtext}

\begin{document}
\begin{paracol}{3}
\switchcolumn[1]
  \everypar{\AtBeginParagraph}

And even there \lmargin{are}{there's something here that I need to pay attention to, and I need to be very clear about it, so I am going to write everything that comes to my mind here in the margin so that it doesn't take anyone 400 years to come up with the same thing!}  more things that can be said, but I am \rmargin{not sure}{Same thing here: there's something here that I need to pay attention to, and I need to be very clear about it, so I am going to write everything that comes to my mind here in the margin so that it doesn't take anyone 400 years to come up with the same thing!}  exactly what. \lipsum[7] 
\lmargin{test}{test}

Do you see the problem \rmargin{here}{And this is what I need}? If I don't go to a new paragraph, everything changes altogether! and overlaps even when I do!

\end{paracol}
\end{document}

演示

答案2

以下链接

http://www.texample.net/tikz/examples/todo-notes/

有帮助吗?

可能除了 todo-notes 之外还需要更多。应该考虑 http://www.texample.net/tikz/examples/global-nodes/

对此的修改,远非完美,但有些安慰,是以下代码,现在定义了两个用于左边距和右边距的宏,将正文中突出显示的文本和备注文本作为参数:

      \documentclass{scrartcl}
      \usepackage{ccfonts}
      \renewcommand{\bfdefault}{sbc}
      \usepackage[T1]{fontenc}
      \usepackage[utf8]{inputenc}
      \usepackage{tikz}
      \usetikzlibrary{calc}
      \newcommand{\tikzrmargin}[2]{%
        \tikz[anchor=base, baseline]{\node [fill=blue!20] (rm) {#1};}
        \begin{tikzpicture}[overlay]
        \coordinate (rightbottom) at ($(rm)+(\paperwidth,0)$);
        \coordinate (se1) at (current page.south east);
        \coordinate (ne1) at (current page.north east);
        \coordinate (rightmargin) at (intersection of rm--rightbottom and se1--ne1);
        \node[left,align=flush right ,text width=2cm, fill=red!20] at (rightmargin)  {\color{gray!70}\footnotesize\bfseries{#2}};
        \end{tikzpicture}
      }
      \newcommand{\tikzlmargin}[2]{%
        \tikz[anchor=base, baseline]{\node [fill=blue!20] (lm) {#1};}
        \begin{tikzpicture}[overlay]
        \coordinate (leftbottom) at ($(lm)-(\paperwidth,0)$);
        \coordinate (sw1) at (current page.south west);
        \coordinate (nw1) at (current page.north west);
        \coordinate (leftmargin) at (intersection of lm--leftbottom and sw1--nw1);
        \node[right,align=flush left ,text width=2cm, fill=green!20] at (leftmargin)  {\color{gray!70}\footnotesize\bfseries{#2}};
        \end{tikzpicture}
      }
      \begin{document}
      \pagestyle{empty}
      \tikzstyle{every picture}+=[remember picture]

      \tikzrmargin{Remark in the right margin!}{This is the Remark.} bla bla bla bla bla gjqp bla bla bla bla blabla bla bla
      bla bla blabla bla blabla bla blabla bla blabla bla blabla bla bla bla bla bla bla bla blabla bla bla bla bla blabla bla bla \tikzlmargin{Remark in the left margin!}{This is the Remark.}
      bla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla bla bla bla blabla bla bla bla bla blabla bla bla
      bla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla bla bla bla blabla bla bla bla bla blabla bla bla
      bla bla blabla bla blabla bla \tikzrmargin{ 2nd remark in the right margin!}{This is the 2\textsuperscript{nd} remark}blabla bla blabla bla blabla bla blabla bla bla bla bla blabla bla bla bla bla blabla bla bla
      bla bla blabla bla blabla bla blabla bla blabla bla blabla bla bla

      This should be put inside a suitable macro! \tikzlmargin{On the left}{again!}



      \end{document}

相关内容