# Using Tikz for framing text

I'd like to use the very appealing example in http://www.texample.net/tikz/examples/framed-tikz/ for framing (and by this highlighting) text. It's nice that this example supports page breaks.

In contrast to the example I want the frame to have the width \textwidth. The straightforward solution is to set inner sep=2em.

But now, of course, I want to have the text be separated from the frame borders. This renders the straightforward solution invalid (or broken).

Does anybody know how to solve my struggle? Any hints are appreciated.

• Many similar such questions on the site. Perhaps start with How to create Highlight Boxes in LaTeX? and What are the relative strong and weak points between tcolorbox and mdframed?. – Alan Munn Aug 4 '16 at 21:10
• Welcome to TeX.SE. I'd recomment using mdframed or tcolorbox and adding a negative leftmargin and \rightmargin. If that does not solve your problem, It would be helpful if you composed a fully compilable MWE including \documentclass and the appropriate packages that sets up the problem. While solving problems can be fun, setting them up is not. Then, those trying to help can simply cut and paste your MWE and get started on solving the problem. – Peter Grill Aug 4 '16 at 21:21
• @PeterGrill These are good suggestions for the generic problem, but is there some particular reason to avoid framed since it is obviously easier not to have to start from scratch! – cfr Aug 4 '16 at 22:16
• @AlanMunn Again, these are useful suggestions in general, but is there some reason to avoid framed? I don't quite understand why everyone wants to send the OP back to the drawing board when the code linked gives almost what is wanted and can easily be adapted to meet the additional desiderata. Is there something I just don't know about this package? – cfr Aug 4 '16 at 22:17
• @cfr I didn't click on the link. If the OP had posted a MWE then I might have worked with that. Without such an example, the question seemed to be more general. I have no opinion on the relative merits of any of the packages in question. – Alan Munn Aug 4 '16 at 22:23

Documentation for packages often tells you useful stuff like how to stop it from blowing up your computer or how to get the software to do exactly what you want.

I've never used framed before, that I recall, but its manual is very helpful, so I can just cut and paste the command required from page 2 into the code of the example.

I've also tidied up the code a bit to eliminate some spurious spaces, to use the backgrounds library and to prefer \newcommand to \def on grounds of safety. There is no need for \def when defining the various new macros required.

[Apparently, nobody ran this with showframe or I assume they would have noticed that all the text was shifted to the right. Presumably, everybody - including the example's author - also ignored all of the bad box warnings LaTeX gave by way of complaint.]

I've also added geometry so that the layout and paper size will correspond and work correctly, microtype to reduce the bad boxes and a slightly increased text width and height, for the same reason.

% Modified from:
%   Nice shaded/framed paragraphs using tikz and framed
%   Author: Jose Luis Diaz
%   url: http://www.texample.net/tikz/examples/framed-tikz/
\documentclass[a5paper]{article}
\usepackage{lipsum}   % To generate test text
\usepackage{geometry}
\usepackage{microtype}
\usepackage{framed}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,calc,backgrounds}
\pgfmathsetseed{1} % To have predictable results
\geometry{scale=.75}

% define styles for the normal border and the torn border
\tikzset{
normal border/.style={orange!30!black!10, decorate, decoration={random steps, segment length=2.5cm, amplitude=.7mm}},
torn border/.style={orange!30!black!5, decorate, decoration={random steps, segment length=.5cm, amplitude=1.7mm}},
}

% Macro to draw the shape behind the text, when it fits completly in the
% page
\newcommand*\parchmentframe[1]{%
\tikz{%
\node[inner sep=2em] (A) {#1};  % Draw the text of the node
\begin{scope}[on background layer]  % Draw the shape behind
\fill[normal border] (A.south east) -- (A.south west) -- (A.north west) -- (A.north east) -- cycle;
\end{scope}
}%
}

% Macro to draw the shape, when the text will continue in next page
\newcommand*\parchmentframetop[1]{%
\tikz{%
\node[inner sep=2em] (A) {#1};    % Draw the text of the node
\begin{scope}[on background layer]
\fill[normal border]              % Draw the complete shape'' behind
(A.south east) -- (A.south west) -- (A.north west) -- (A.north east) -- cycle;
\fill[torn border]                % Add the torn lower border
($(A.south east)-(0,.2)$) -- ($(A.south west)-(0,.2)$) -- ($(A.south west)+(0,.2)$) -- ($(A.south east)+(0,.2)$) -- cycle;
\end{scope}
}%
}

% Macro to draw the shape, when the text continues from previous page
\newcommand*\parchmentframebottom[1]{%
\tikz{%
\node[inner sep=2em] (A) {#1};   % Draw the text of the node
\begin{scope}[on background layer]
\fill[normal border]             % Draw the complete shape'' behind
(A.south east) -- (A.south west) -- (A.north west) -- (A.north east) -- cycle;
\fill[torn border]               % Add the torn upper border
($(A.north east)-(0,.2)$) -- ($(A.north west)-(0,.2)$) -- ($(A.north west)+(0,.2)$) -- ($(A.north east)+(0,.2)$) -- cycle;
\end{scope}
}%
}

% Macro to draw the shape, when both the text continues from previous page
% and it will continue in next page
\newcommand*\parchmentframemiddle[1]{%
\tikz{%
\node[inner sep=2em] (A) {#1};   % Draw the text of the node
\begin{scope}[on background layer]
\fill[normal border]             % Draw the complete shape'' behind
(A.south east) -- (A.south west) -- (A.north west) -- (A.north east) -- cycle;
\fill[torn border]               % Add the torn lower border
($(A.south east)-(0,.2)$) -- ($(A.south west)-(0,.2)$) -- ($(A.south west)+(0,.2)$) -- ($(A.south east)+(0,.2)$) -- cycle;
\fill[torn border]               % Add the torn upper border
($(A.north east)-(0,.2)$) -- ($(A.north west)-(0,.2)$) -- ($(A.north west)+(0,.2)$) -- ($(A.north east)+(0,.2)$) -- cycle;
\end{scope}
}%
}

% Define the environment which puts the frame
% In this case, the environment also accepts an argument with an optional
% title (which defaults to Example'', which is typeset in a box overlaid
% on the top border
\newenvironment{parchment}[1][Example]{%
\def\FrameCommand{\parchmentframe}%
\def\FirstFrameCommand{\parchmentframetop}%
\def\LastFrameCommand{\parchmentframebottom}%
\def\MidFrameCommand{\parchmentframemiddle}%
\vskip\baselineskip
\noindent\tikz{\node[inner sep=1ex, draw=black!20,fill=white, anchor=west, overlay] at (0em, 2em) {\sffamily#1};}\par}%
{\endMakeFramed}

% Main document, example of usage
\pagestyle{empty}
\begin{document}
\begin{parchment}[Short text]
\lipsum[11]
\end{parchment}
\lipsum[11]
\begin{parchment}[Long text spanning over pages]
\lipsum[11]
\begin{itemize}
\item \lipsum[14]
\end{itemize}
\lipsum
\end{parchment}
\end{document}


• Thank you very much! This solves everything I intended to ask for. Of course I need to admit that with your readily adjusted code the second page of the documentation of the package framed looks quite obvious. Shame on me. And of course thank you for tidying up! – F. B. Aug 6 '16 at 19:16