在多頁的頁邊空白處繪製 tikz 線

在多頁的頁邊空白處繪製 tikz 線

受到本網站上找到的程式碼部分的啟發(我不記得了...),我可以執行以下操作在第一頁上從頂部開始畫一條線:

\documentclass[a4paper,12pt]{book}
\usepackage{tikz}
\usepackage{etoolbox}
\usepackage{lipsum}

\begin{document}

\thispagestyle{empty}

\begin{tikzpicture}[remember picture,overlay]
    \node (back names) [shape=rectangle,
    fill=black!80, 
    minimum height=\textheight, 
    minimum width=1cm, 
    anchor=north west] 
    at ([yshift=-(1in+\topmargin+\headheight+\headsep)]current page.north west) {};
\end{tikzpicture}

First line.
Second line.
\lipsum

\end{document}

我想做的是將矩形擴展到長文本的末尾(因此可能需要建立一個“環境”和/或一個帶有考慮文本的參數的新命令),並且能夠準確地從本文的開頭開始並準確地在其結束處結束,即使開頭或結尾位於頁面中間,並且(tiebraker ;))不使用盒子

事實上,我知道如何使用例如 mdframed 並將左欄向左移動......它效果很好,而且很好我們確實知道用framed、mdframed、tcolorbox等創建的兩個可破壞/可分割的盒子不能嵌套(即使是一個與另一個,至少在我嘗試過的情況下)。但是,這就是我想做的:我用 tikz 繪製的線(實際上是大規則)將用作容器,我將能夠在其中放入其他可破碎/可拆分的盒子......

那可能嗎?做過?

編輯:靈感就在這裡:TikZ 全頁絕對節點定位

答案1

這使用 tikzpagenodes、tikzmark 和 everypage。不要忘記運行兩次。

我使用的環境主要是為了使其不易混亂。 OTOH,我忘了解決巢狀環境。

\documentclass[a4paper,12pt]{book}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc,tikzmark}
\usepackage{lipsum}
\usepackage{showframe}
\usepackage{everypage}

\makeatletter
\newcommand{\checkmarkpage}[4]% #1 = tikzmark label, #2 = less, #3 = equal, #4 = greater
{\@ifundefined{save@pt@#1}{#2}{%
  \edef\markid{\csname save@pt@#1\endcsname}%
  \edef\markpage{\csname save@pg@\markid\endcsname}%
  \ifnum\thepage<\markpage\relax #2%
  \else
    \ifnum\thepage=\markpage\relax #3%
    \else #4%
    \fi
  \fi}%
}
\makeatother

\newcounter{outlineid}
\newcounter{outlinedone}

\newenvironment{outline}{\par\tikzmark{begin\theoutlineid}\ignorespaces}%
  {\par\tikzmark{end\theoutlineid}\stepcounter{outlineid}\ignorespaces}

\newcommand{\drawoutline}{\checkmarkpage{begin\theoutlinedone}{}%
  {\begin{tikzpicture}[remember picture,overlay]
    \path ({pic cs:begin\theoutlinedone}-| current page text area.west)
      ++(0pt,\ht\strutbox) coordinate(A);
    \checkmarkpage{end\theoutlinedone}%
      {\path (current page text area.south west) ++(0pt,-\dp\strutbox)
         coordinate(B);}%
      {\path ({pic cs:end\theoutlinedone}-| current page text area.west)
        ++(0pt,\ht\strutbox) coordinate(B);}%
      {}% this should not happen
    \fill[yellow] ($(A) + (-.333em,0pt)$) rectangle ($(B) + (-1cm,0pt)$);
   \end{tikzpicture}}%
  {\begin{tikzpicture}[remember picture,overlay]
    \coordinate (A) at (current page text area.north west);
    \checkmarkpage{end\theoutlinedone}%
      {\path (current page text area.south west) ++(0pt,-\dp\strutbox)
         coordinate(B);}%
      {\path ({pic cs:end\theoutlinedone}-| current page text area.west)
        ++(0pt,\ht\strutbox) coordinate(B);}%
      {}% this should not happen
    \fill[yellow] ($(A) + (-.333em,0pt)$) rectangle ($(B) + (-1cm,0pt)$);
   \end{tikzpicture}}%
  \checkmarkpage{end\theoutlinedone}{}%
    {\stepcounter{outlinedone}\drawoutline}%
    {}% this should not happen
 }
\AddEverypageHook{\drawoutline}

\begin{document}

\newpage 

\thispagestyle{empty}

\lipsum[1]

Next line begins the rule :

\begin{outline}
\lipsum[2]
\end{outline}

Line above ends the rule.

Two on one page:
\begin{outline}
\lipsum[3]
\end{outline}

One on three pages:
\begin{outline}
\lipsum[4-12]
\end{outline}
That's all, folks.

\end{document}

答案2

這是答案的開始。我現在可以從一條任意線到另一條任意線繪製規則在同一頁上。下一步是找到一種方法:

1) 將規則停止在頁尾最後一行之前;

2)轉到下一頁(類似於當前頁+1??);

3)重複1)-2)直到文本結束......

當然還有一兩件事,因為下面程式碼的 \zsavepos{lineend} 沒有考慮到這個位置可能是第一個位置之後的 1, 2, 3... x 頁!以及如何知道這一點?

對我很重要...): 我使用了名為dimtomm的程式碼部分中的一段程式碼(dimwithoutpt)(將pt中的dim轉換為mm),但我並不真正理解dimexpr的語法(我讀了etex-man,但是..... .嗯!如果有人可以向我解釋程式碼的閱讀方式,那就太好了:-)。

也許有一種更乾淨的方法來進行轉換以及 \beg 和 \portionheight 的設置,但我一點也不熟悉這種操作...

\documentclass[a4paper,12pt]{book}
\usepackage{tikz}
\usepackage{etoolbox}
\usepackage{lipsum}
\usepackage{showframe}
\usepackage{layout}
\usepackage{zref-savepos}

\makeatletter
\newcommand\dimwithoutpt[1]{%
    \strip@pt\dimexpr 1\dimexpr#1\relax\relax%
}
\makeatother

\newlength\beg
\newlength\portionheight

\begin{document}
\layout{}

\newpage 

\thispagestyle{empty}

\lipsum[1]

Next line begins the rule :

\zsavepos{linedeb}Begining of my new section.

\setlength{\beg}{dimwithoutpt{\paperheight-\zposy{linedeb}sp}pt}

\lipsum[2]\zsavepos{lineend} 
\setlength{\portionheight}{\dimwithoutpt{\zposy{linedeb}sp-\zposy{lineend}sp}pt}

\begin{tikzpicture}[remember picture,overlay]
    \node (back names) [shape=rectangle, fill=yellow, minimum height=\portionheight, minimum width=1cm, anchor=north west] at ([yshift=-\beg,xshift=1in+\oddsidemargin-1cm]current page.north west) {};
\end{tikzpicture}
Line above ends the rule.

And next : 

\lipsum[3]

\end{document}

相關內容