广告 Tikz 填充矩形,用作原始文档中块文本的背景

广告 Tikz 填充矩形,用作原始文档中块文本的背景

是否可以将 Tikz 填充的矩形放在用作背景颜色的文本块后面?尽管文本位于 Tikz 节点内,但文本块的边距和位置应保持不变,即矩形应适应文本位置/边距。

原始文件示例 在此处输入图片描述

添加背景颜色后 在此处输入图片描述

有可能吗?

来源:如何制作我自己的 LaTeX 模板?

答案1

像这样吗?编辑:填充、混合模式,使框能够跨越分页符。

\documentclass{article}
\usepackage{tikz}
\usepackage{refcount}
\usetikzlibrary{backgrounds,calc}
\usepackage{tikzpagenodes}
\usepackage{lipsum}
\tikzset{SimpleBox/.style={draw,red,thick,fill=blue,fill opacity=0.3,rounded
corners}}



\newcounter{tmp}

\newcommand{\StartBox}[1][]{%
  \stepcounter{tmp}%from https://tex.stackexchange.com/a/52579/121799
  \tikz[remember picture,overlay, baseline=(Begin.base)]{%
  \node[anchor=base,inner sep=0pt,outer sep=0pt] (Begin) {\strut};}
  \label{a\thetmp}%
  \ifnum\getpagerefnumber{a\thetmp}=\getpagerefnumber{b\thetmp} \else
  \begin{tikzpicture}[overlay, remember picture]
   \path (current page text area.north west) -- (current page text area.south west)
    node(WestLine)[left]{};
   \path (current page text area.north east) -- (current page text area.south east)
    node(EastLine)[right]{};
   \begin{scope}[on background layer,blend mode=multiply]
   \draw[SimpleBox,#1] ($(current page text area.south-|WestLine)-(0,2pt)$)
   --      (Begin.north-|WestLine) -- (Begin.north-|EastLine) 
   --  ($(current page text area.south-|EastLine)-(0,2pt)$);
   \end{scope}
  \end{tikzpicture}%
  \fi%
}

\newcommand{\EndBox}[1][]{%
\tikz[remember picture,overlay, baseline=(End.base)]{%
\node[anchor=base,inner sep=0pt,outer sep=0pt] (End) {\strut};}%
  \label{b\thetmp}
  \ifnum\getpagerefnumber{a\thetmp}=\getpagerefnumber{b\thetmp}
  \begin{tikzpicture}[overlay,remember picture]
   \path (current page text area.north west) -- (current page text area.south west)
    node(WestLine)[left]{};
   \path (current page text area.north east) -- (current page text area.south east)
    node(EastLine)[right]{};
   \begin{scope}[on background layer,blend mode=multiply]
    \draw[SimpleBox,#1] (Begin.north-|WestLine) rectangle (End.south-|EastLine);
   \end{scope}
  \end{tikzpicture}
  \else
  \begin{tikzpicture}[overlay,remember picture]
   \path (current page text area.north west) -- (current page text area.south west)
    node(WestLine)[left]{};
   \path (current page text area.north east) -- (current page text area.south east)
    node(EastLine)[right]{};
   \begin{scope}[on background layer,blend mode=multiply]
   \draw[SimpleBox,#1] ($(current page text area.north-|WestLine)+(0,2pt)$)
   --      (End.south-|WestLine) -- (End.south-|EastLine) 
   --  ($(current page text area.north-|EastLine)-(0,2pt)$);
   \end{scope}
  \end{tikzpicture}
  \fi
}

\newcommand{\SimpleBox}[2][]{%
\StartBox[#1]%
#2\EndBox[#1]}


\begin{document}

\SimpleBox{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed a leo ut
erat luctus scelerisque. Suspendisse auctor mauris sit amet fringilla
hendrerit. Maecenas eu tortor eu diam fringilla scelerisque sit amet eget enim.
Nam vitae bibendum erat, sit amet scelerisque felis. Suspendisse placerat
vitae velit pharetra lobortis et marmottae. Donec ut erat a erat porta aliquet. Nulla eget
augue sem. Proin orci neque, eleifend id eleifend id, venenatis sit amet diam.
Mauris lacus est, cursus eget sapien at, pharetra lobortis ante. Proin
tempus ipsum tellus, ut ultricies magna congue nec. Vestibulum ante ipsum primis
in faucibus orci luctus et ultrices posuere cubilia Curae; Suspendisse potenti.
In posuere dapibus urna, vitae euismod magna iaculis at. Pellentesque pretium,
lorem ut vehicula egestas, velit dolor porta nunc, id interdum ipsum purus vel
dolor.}

\lipsum[2]

\tikzset{SimpleBox/.style={draw,blue,thick,fill=red,fill opacity=0.3}}

\SimpleBox{\lipsum[3]}

\lipsum[4]


\SimpleBox[draw=yellow,fill=yellow]{Wanna yellow?}


Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed a leo ut
erat luctus scelerisque. Suspendisse auctor mauris sit amet fringilla
hendrerit. Maecenas eu tortor eu diam fringilla scelerisque sit amet eget enim.
Nam vitae bibendum erat, sit amet scelerisque felis. Suspendisse placerat
vitae velit pharetra lobortis et marmottae. Donec ut erat a erat porta aliquet. Nulla eget
augue sem.

\SimpleBox{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed a leo ut
erat luctus scelerisque. Suspendisse auctor mauris sit amet fringilla
hendrerit. Maecenas eu tortor eu diam fringilla scelerisque sit amet eget enim.
Nam vitae bibendum erat, sit amet scelerisque felis. Suspendisse placerat
vitae velit pharetra lobortis et marmottae. Donec ut erat a erat porta aliquet. Nulla eget
augue sem. Proin orci neque, eleifend id eleifend id, venenatis sit amet diam.
Mauris lacus est, cursus eget sapien at, pharetra lobortis ante. Proin
tempus ipsum tellus, ut ultricies magna congue nec. Vestibulum ante ipsum primis
in faucibus orci luctus et ultrices posuere cubilia Curae; Suspendisse potenti.
In posuere dapibus urna, vitae euismod magna iaculis at. Pellentesque pretium,
lorem ut vehicula egestas, velit dolor porta nunc, id interdum ipsum purus vel
dolor.}
\end{document}

在此处输入图片描述

答案2

要用背景颜色或带框架的背景颜色注释某些段落,您不需要使用tikzhackary ...使用提供的颜色框tcolorbox可以得到相同的结果:

在此处输入图片描述

母语:

\documentclass{article}
% for indentation of a text in mini page
\parskip=1ex    %  just drop if you not like it
\newcommand{\saveparinfos}{%
\edef\myindent{\the\parindent}%
\edef\myparskip{\the\parskip}}

\newcommand{\useparinfo}{%
\setlength{\parindent}{\myindent}%
\setlength{\parskip}{\myparskip}}
\saveparinfos
% color boxes
\usepackage{xcolor}
\newcommand\ColorBox[2]%
{\noindent\hspace*{-\dimexpr2\fboxsep+2\fboxrule\relax}\mbox{
\fcolorbox{#1}{#1}{%
\begin{minipage}{\linewidth}%
#2
\end{minipage}}}\par
}
\newcommand\ColorFBox[2]%
{\noindent\hspace*{-\dimexpr2\fboxsep+2\fboxrule\relax}
 \setlength\fboxrule{1pt}
\fcolorbox{#1}{#1!30}{%
\begin{minipage}{\linewidth}
#2
\end{minipage}}\par
}

% dummy text
\usepackage{lipsum}

\begin{document}
\lipsum[1]
\ColorBox{blue!30}{\useparinfo % in case when you like to have 
                                % in minipage the same indentation
                                % as it is main text
\lipsum[11]\lipsum[12]
                 }
\lipsum[11]
\ColorFBox{red}{\lipsum[12]}
\end{document}

笔记:框的内容(以及tikz节点)不能分成两页。为此,您应该考虑tcolorbox包装)

相关内容