根据一些资料,我可以编写以下代码来创建撕纸效果:
\documentclass[final]{book}
\usepackage[paperwidth=5in,paperheight=8in,top=0.25in,bottom=0.3in,inner=0.5in,outer=0.5in,bindingoffset=0.25in,includefoot,includehead]{geometry}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\definecolor{paper}{RGB}{238,233,233}
\def \tornpaper#1#2#3#4{
\begin{tikzpicture}[pencildraw/.style={ %
decorate,
decoration={random steps,segment length=2pt,amplitude=1pt}
}%
]
\node[
preaction={fill=black,opacity=.5,transform canvas={xshift=2mm,yshift=-2mm}},
pencildraw,draw,fill=paper,text width=0.8\textwidth,inner sep=9mm]
{#1\\~~~~#2\\~~~~#3\\~~~~#4};
\end{tikzpicture}
}
\begin{document}
\tornpaper{First paragraph}{2nd paragraph. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquam consequatur quod iste ratione in, enim nulla ducimus eum, expedita non? Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates, praesentium!}{3nd paragraph. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eos facilis eligendi, deleniti excepturi modi asperiores nihil harum ratione qui reiciendis.}{Last paragraph. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Omnis, recusandae.}
\end{document}
强制换行和不间断空格的使用方式很奇怪,因为(据我所知)这是在此 tikz 绘图内创建带有缩进的新段落的唯一方法。
上述绘图定义与我的geometry
设置一起生成位于页面右边距的绘图。我想将绘图移离边缘(最好是移到页面的中心)。我没能找到任何在tikz
手册中执行此操作的方法。我甚至尝试过围绕绘图创建另一个环境来添加一些边距,还尝试过使用newgeometry
命令来更改页边距(并以此方式将绘图移近页面中心)。
有没有什么办法可以控制该绘图周围的边距,而不改变绘图宽度?
答案1
以下示例扩展了 Peter Grill 的评论:
- 文本宽度尽可能计算。(粗糙的外线中存在随机元素。)
如果\parindent
不设置为零,则段落的第一行将缩进\parindent
。由于tikzpicture
环境开始新段落的第一行,因此它会缩进。\noindent
删除此段落的缩进。
\documentclass[final]{book}
\usepackage[
paperwidth=5in,
paperheight=8in,
top=0.25in,
bottom=0.3in,
inner=0.5in,
outer=0.5in,
bindingoffset=0.25in,
includefoot,
includehead
]{geometry}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\definecolor{paper}{RGB}{238,233,233}
\newlength{\torninnersep}
\setlength{\torninnersep}{9mm}
\newlength{\tornamplitude}
\setlength{\tornamplitude}{1pt}
\newlength{\tornshift}
\setlength{\tornshift}{2mm}
\newcommand\tornpaper[4]{%
\noindent
\begin{tikzpicture}[
pencildraw/.style={%
decorate,
decoration={
random steps,
segment length=2pt,
amplitude=\tornamplitude
}
}%
]
\node[
preaction={
fill=black,
opacity=.5,
transform canvas={xshift=\tornshift,yshift=-\tornshift}
},
pencildraw,
draw,
fill=paper,
text width=\dimexpr
\linewidth
-2\torninnersep
-2\tornamplitude
-\pgflinewidth
\relax,
inner sep=\torninnersep,
] {#1\\~~~~#2\\~~~~#3\\~~~~#4};
\end{tikzpicture}%
}
\begin{document}
% Visual marker for the line width
\hrule height 2pt
\medskip
\tornpaper{First paragraph}{2nd paragraph. Lorem ipsum dolor sit amet,
consectetur adipisicing elit. Aliquam consequatur quod iste ratione in, enim
nulla ducimus eum, expedita non? Lorem ipsum dolor sit amet, consectetur
adipisicing elit. Voluptates, praesentium!}{3nd paragraph. Lorem ipsum dolor
sit amet, consectetur adipisicing elit. Eos facilis eligendi, deleniti
excepturi modi asperiores nihil harum ratione qui reiciendis.}{Last
paragraph. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Omnis,
recusandae.}
\end{document}
可以看出,阴影绘制在外面。因此,它粘在右边距和下面。以下示例将阴影放在框内,TeX 可以看到\tornpaper
:
\documentclass[final]{book}
\usepackage[
paperwidth=5in,
paperheight=8in,
top=0.25in,
bottom=0.3in,
inner=0.5in,
outer=0.5in,
bindingoffset=0.25in,
includefoot,
includehead
]{geometry}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\definecolor{paper}{RGB}{238,233,233}
\newlength{\torninnersep}
\setlength{\torninnersep}{9mm}
\newlength{\tornamplitude}
\setlength{\tornamplitude}{1pt}
\newlength{\tornshift}
\setlength{\tornshift}{2mm}
\newcommand\tornpaper[4]{%
\sbox0{%
\hbox to \linewidth{\hss
\begin{tikzpicture}[
pencildraw/.style={%
decorate,
decoration={
random steps,
segment length=2pt,
amplitude=\tornamplitude
}
}%
]
\node[
preaction={
fill=black,
opacity=.5,
transform canvas={xshift=\tornshift,yshift=-\tornshift}
},
pencildraw,
draw,
fill=paper,
text width=\dimexpr
\linewidth
-2\torninnersep
-2\tornamplitude
-\pgflinewidth
-\tornshift
\relax,
inner sep=\torninnersep,
] {#1\\~~~~#2\\~~~~#3\\~~~~#4};
\end{tikzpicture}%
\kern\tornshift
\hss
}%
}%
\dp0=\dimexpr\dp0 + \tornshift\relax
\noindent
\usebox{0}%
}
\begin{document}
% Visual marker for the line width
\hrule height 2pt
\medskip
\tornpaper{First paragraph}{2nd paragraph. Lorem ipsum dolor sit amet,
consectetur adipisicing elit. Aliquam consequatur quod iste ratione in, enim
nulla ducimus eum, expedita non? Lorem ipsum dolor sit amet, consectetur
adipisicing elit. Voluptates, praesentium!}{3nd paragraph. Lorem ipsum dolor
sit amet, consectetur adipisicing elit. Eos facilis eligendi, deleniti
excepturi modi asperiores nihil harum ratione qui reiciendis.}{Last
paragraph. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Omnis,
recusandae.}
\end{document}