使用 Tikz 来为文本添加框架

使用 Tikz 来为文本添加框架

我想用一个非常吸引人的例子 http://www.texample.net/tikz/examples/framed-tikz/ 用于框定(并通过此突出显示)文本。此示例支持分页符,这很好。

与示例相反,我希望框架具有宽度\textwidth。直接的解决方案是设置 inner sep=2em

但现在,我当然想让文本与框架边框分开。这使得直接的解决方案无效(或失败)。

有人知道如何解决我的问题吗?任何提示都值得感激。

答案1

软件包的文档通常会告诉您一些有用的内容,例如如何阻止它炸毁您的计算机或如何让软件执行您想要的操作。

我记得我以前从未使用过framed,但它的手册非常有用,所以我可以直接从第 2 页剪切并粘贴所需的命令到示例代码中。

我还对代码进行了一些整理,以消除一些不必要的空格,以便使用库并出于安全考虑而backgrounds选择。在定义所需的各种新宏时不需要。\newcommand\def\def

[显然,没有人运行过这个程序showframe,或者我认为他们会注意到所有文本都向右移动了。大概每个人(包括示例的作者)也忽略了 LaTeX 以投诉方式给出的所有坏框警告。]

出于同样的原因,我还添加了geometry以使布局和纸张尺寸对应并正常工作,microtype以减少坏框和略微增加的文本宽度和高度。

% 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
  \MakeFramed {\advance\hsize-\width\FrameRestore}%
  \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}

调整后的羊皮纸

相关内容