我尝试在底部设置一个tcolorbox
撕纸效果。我查看了这些问题和答案:
最后,我得到了下面的代码:
\documentclass{article}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,shadows.blur,shadings}
%\pgfmathsetseed{1} % To have predictable results
\usepackage[most]{tcolorbox}
% Define a background layer, in which the parchment shape is drawn
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
% This is the base for the fractal decoration. It takes a random point
% between the start and end, and raises it a random amount, thus
% transforming a segment into two, connected at that raised point This
% decoration can be applied again to each one of the resulting
% segments and so on, in a similar way of a Koch snowflake.
\pgfdeclaredecoration{irregular fractal line}{init}
{
\state{init}[width=\pgfdecoratedinputsegmentremainingdistance]
{
\pgfpathlineto{%
\pgfpoint{random * \pgfdecoratedinputsegmentremainingdistance}{%
(random * \pgfdecorationsegmentamplitude - 0.02) *
\pgfdecoratedinputsegmentremainingdistance}}
\pgfpathlineto{\pgfpoint{\pgfdecoratedinputsegmentremainingdistance}{0pt}}
}
}
% Define paper style
\tikzset{
paper/.style={%
draw=black!10,
blur shadow,
path picture={%
\node at (path picture bounding box.center) {
\includegraphics[width=\linewidth]{note-background.png}
};
}
},
irregular border/.style={%
decoration={irregular fractal line, amplitude=0.2},
decorate,
},
ragged border/.style={%
decoration={random steps, segment length=7mm, amplitude=2mm},
decorate,
}
}
\newtcolorbox{tcbnote}{%
% enhanced jigsaw, <-- Beware, This rendering engine let the
% frame appear on some PDF viewers
empty, % Better use this rendering engine to avoid the bug
interior code={%
\begin{pgfonlayer}{background} % Draw the shape behind
\fill[paper] % recursively decorate the bottom border
decorate[irregular border]{%
decorate{decorate{decorate{decorate[ragged border]{
($(interior.south east) - (0, random * 5mm)$) --
($(interior.south west) - (0, random * 5mm)$)
}
}
}
}
}
-- (interior.north west) -- (interior.north east) -- cycle;
\end{pgfonlayer}
},
sharp corners,
boxrule=0pt,
left=5pt,
right=5pt,
top=5pt,
bottom=1em,
}
%% TColorBox Note environment
\newlength\savedparindent
\setlength\savedparindent\parindent
\newenvironment{note}[1]{%
\begin{tcbnote}{}
\setlength\parindent{\savedparindent}
\begin{center}
\sffamily\bfseries\LARGE #1
\end{center}\par
}{%
\end{tcbnote}
}
\begin{document}
\pagestyle{empty}
\begin{note}{Note Title}
\lipsum[1-2]
\end{note}
\end{document}
这里使用的背景图像如下。
最终结果如下。
这完全符合我最初的期望,但由于几个原因(见下文),我对我编写的 LaTeX 代码并不满意。因此,我请求一些提示和建议来改进它。
我的主要问题是:
我没有设法使用本机标题,
tcolorbox
因为我确实发现如何扩展背景以连续覆盖盒子的第一部分;[解决了,感谢帐篷]
\parindent
变量似乎在此环境中被重置为零,我不明白为什么会发生这种情况。我确实找到了一种解决方法,即\parindent
在环境中保存并恢复它。但这只是一个糟糕的黑客行为。任何其他改进想法都值得欢迎!
答案1
以下代码对初始代码进行了一些更改。
tcbnote
环境有两个参数(可选+强制)。- title box 是必选参数,不需要环境
note
。 - 标题与选项一起放置
attach boxed title to top center
。这样背景图像就覆盖了标题和内容。 \parindent
值是通过before upper
选项固定的。- 可选参数允许引入更改,而无需声明新的 tcolorbox
这是代码:
\documentclass{article}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,shadows.blur,shadings}
%\pgfmathsetseed{1} % To have predictable results
\usepackage[most]{tcolorbox}
% Define a background layer, in which the parchment shape is drawn
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
% This is the base for the fractal decoration. It takes a random point
% between the start and end, and raises it a random amount, thus
% transforming a segment into two, connected at that raised point This
% decoration can be applied again to each one of the resulting
% segments and so on, in a similar way of a Koch snowflake.
\pgfdeclaredecoration{irregular fractal line}{init}
{
\state{init}[width=\pgfdecoratedinputsegmentremainingdistance]
{
\pgfpathlineto{%
\pgfpoint{random * \pgfdecoratedinputsegmentremainingdistance}{%
(random * \pgfdecorationsegmentamplitude - 0.02) *
\pgfdecoratedinputsegmentremainingdistance}}
\pgfpathlineto{\pgfpoint{\pgfdecoratedinputsegmentremainingdistance}{0pt}}
}
}
% Define paper style
\tikzset{
paper/.style={%
draw=black!10,
blur shadow,
path picture={%
\node at (path picture bounding box.center) {
\includegraphics[width=\linewidth]{note-background.png}
};
}
},
irregular border/.style={%
decoration={irregular fractal line, amplitude=0.2},
decorate,
},
ragged border/.style={%
decoration={random steps, segment length=7mm, amplitude=2mm},
decorate,
}
}
\newtcolorbox{tcbnote}[2][]{%
% enhanced jigsaw, <-- Beware, This rendering engine let the
% frame appear on some PDF viewers
empty, % Better use this rendering engine to avoid the bug
interior code={%
\begin{pgfonlayer}{background} % Draw the shape behind
\fill[paper] % recursively decorate the bottom border
decorate[irregular border]{%
decorate{decorate{decorate{decorate[ragged border]{
($(interior.south east) - (0, random * 5mm)$) --
($(interior.south west) - (0, random * 5mm)$)
}
}
}
}
}
-- (interior.north west) -- (interior.north east) -- cycle;
\end{pgfonlayer}
},
sharp corners,
boxrule=0pt,
left=5pt,
right=5pt,
top=5pt,
bottom=1em,
fonttitle=\sffamily\bfseries\LARGE,
attach boxed title to top center={yshift*=-\tcboxedtitleheight},
coltitle=black,
before upper={\setlength\parindent{\savedparindent}},
title=#2,
#1
}
%% TColorBox Note environment
\newlength\savedparindent
\setlength\savedparindent\parindent
\begin{document}
\pagestyle{empty}
\begin{tcbnote}{Note Title}
\lipsum[1-2]
\end{tcbnote}
\end{document}