2

Based on few sources, I have been able to write following code to create torn paper effect:

\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}

Forced line breaks and non-breaking spaces are used in such weird manner, because (according to my knowledge) it is only way to create new paragraphs with indents inside this tikz drawing.

Above definition of drawing, together with my geometry settings produce drawing that is on the right margin of page. I would like to move drawing further away from margin (ideally to the center of the page). I have not been able to find any way to do it in tikz manual. I have even tried to create another environment around drawing to add some margins and also tried to use newgeometry command to change page margins (and move drawing closer to center of the page that way).

Is there any way to control margins around this drawing, without changing drawing width?

2
  • 1
    Adding a \noindent before the \tornpaper, and changing your inner sep=8mm seems to allow the \tornpaper to fit. – Peter Grill Dec 5 '14 at 18:00
  • @PeterGrill Thanks! Adding \noindent pushes whole drawing towards left margin (which is basically what I wanted to do). Do you know why it works? Only explaination I can see is that tikz drawing is treated by LaTeX like text and \noindent removes first line indent here. Am I right? – Rafal Dec 5 '14 at 19:22
3

The following examples extends Peter Grill's comment:

  • The text width is calculated as far as possible. (There is a random element in the ragged outer line.)

If \parindent is not set to zero, the first line of a paragraph is indented by \parindent. Since the tikzpicture environment starts the first line of a new paragraph, it is indented. \noindent removes the indentation for this paragraph.

\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}

Result

As can be seen, the shadow is drawn outside. Thus it sticks into the right margin and below. The following example puts the shadow inside the box, seen by TeX for \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}

Result with shadow inside

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.