页面之间的方框文字

页面之间的方框文字

tikz我按照以下示例将方框文本制作成了手稿例子,只要盒子足够短,可以放入一页,它就会像魔法一样发挥作用。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,snakes}
\usepackage{amsmath,amssymb}
\begin{document}

% Define box and box title style
\tikzstyle{mybox} = [draw=red, fill=blue!20, very thick,
    rectangle, rounded corners, inner sep=10pt, inner ysep=20pt]
\tikzstyle{fancytitle} =[fill=red, text=white]

\begin{tikzpicture}
\node [mybox] (box){%
    \begin{minipage}{0.50\textwidth}
        To calculate the horizontal position the kinematic differential
        equations are needed:
        \begin{align}
            \dot{n} &= u\cos\psi -v\sin\psi \\
            \dot{e} &= u\sin\psi + v\cos\psi
        \end{align}
        For small angles the following approximation can be used:
        \begin{align}
            \dot{n} &= u -v\delta_\psi \\
            \dot{e} &= u\delta_\psi + v
        \end{align}
    \end{minipage}
};
\node[fancytitle, right=10pt] at (box.north west) {A fancy title};
\node[fancytitle, rounded corners] at (box.east) {$\clubsuit$};
\end{tikzpicture}%
%
\end{document}

问题

有没有办法扩展代码,使框能够(自动)跨页?

我刚刚找到了这个mdframed包,我该如何合并这两个包呢?

答案1

第一版本:一般的盒装mdframed

你不需要combine两个包,只需加载mdframedframemethod=TikZ查看示例 5mdframed-example-tikz.pdf。我仍然不完全了解它是如何工作的,但经过反复试验后,我编写了下一个代码。

\documentclass{article}
\usepackage{tikz}
%\usetikzlibrary{shapes}
\usepackage{amsmath,amssymb}
\usepackage{lipsum}
\usepackage[framemethod=TikZ]{mdframed}

\mdfsetup{skipabove=\topskip,skipbelow=\topskip}

\tikzstyle{titregris} =
    [draw=red, thick, fill=red,% 
        text=white, rectangle,  
        right,minimum height=.7cm]

\pgfdeclarehorizontalshading{exercisebackground}{100bp}{color(0bp)=(green!40);color(100bp)=(black!5)}

\newcounter{exercise}

\renewcommand*\theexercise{Exercise~n\arabic{exercise}}


\makeatletter
\def\mdf@@exercisepoints{}
\define@key{mdf}{exercisepoints}{
    \def\mdf@exercisepoints{#1}
}
\def\mdf@@myboxedtitle{}
\define@key{mdf}{myboxedtitle}{
    \def\mdf@myboxedtitle{#1}
}
\mdfdefinestyle{exercisestyle}{%
    outerlinewidth=1em,%
    outerlinecolor=white,%
    leftmargin=-1em,%
    rightmargin=-1em,%
    middlelinewidth=1.2pt,%
    roundcorner=5pt,%
    linecolor=red,%
    apptotikzsetting={\tikzset{mdfbackground/.append style={blue!30}}},
    innertopmargin=1.2\baselineskip,
    skipabove={\dimexpr0.5\baselineskip+\topskip\relax},
    skipbelow={-1em},
    needspace=3\baselineskip,
    frametitlefont=\sffamily\bfseries,
    settings={\global\stepcounter{exercise}},
    singleextra={%
        \node[titregris,xshift=1cm] at (P-|O) %
            {~\mdf@frametitlefont{\mdf@myboxedtitle}~};
},%
    firstextra={%
\node[titregris,xshift=1cm] at (P-|O) %
{~\mdf@frametitlefont{\mdf@myboxedtitle}~};
},
}
\makeatother


\begin{document}

\lipsum[1-4]

\begin{mdframed}[style=exercisestyle,myboxedtitle={Hello}]
        To calculate the horizontal position the kinematic differential
        equations are needed:
        \begin{align}
            \dot{n} &= u\cos\psi -v\sin\psi \\
            \dot{e} &= u\sin\psi + v\cos\psi
        \end{align}
        For small angles the following approximation can be used:
        \begin{align}
            \dot{n} &= u -v\delta_\psi \\
            \dot{e} &= u\delta_\psi + v
        \end{align}
\end{mdframed}

\end{document}

结果是:

在此处输入图片描述

第二版本:锻炼盒装mdframed

正如我所说,之前的代码取自mdframed文档中的一个示例。此示例应该用于练习,我没有更改原始名称。现在,如果您想用这种格式输入练习,我们可以回到原始代码,稍微清理一下并获得:

\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage[framemethod=TikZ]{mdframed}

\tikzstyle{titlered} =
    [draw=red, thick, fill=red,% 
        text=white, rectangle,  
        right, minimum height=.7cm]

\newcounter{exercise}

\renewcommand*\theexercise{Exercise~\arabic{exercise}}

\makeatletter
\mdfdefinestyle{exercisestyle}{%
    outerlinewidth=1em,%
    outerlinecolor=white,%
    leftmargin=-1em,%
    rightmargin=-1em,%
    middlelinewidth=1.2pt,%
    roundcorner=5pt,%
    linecolor=red,%
    backgroundcolor=blue!20,
    innertopmargin=1.2\baselineskip,
    skipabove={\dimexpr0.5\baselineskip+\topskip\relax},
    skipbelow={-1em},
    needspace=3\baselineskip,
    frametitlefont=\sffamily\bfseries,
    settings={\global\stepcounter{exercise}},
    singleextra={%
        \node[titlered,xshift=1cm] at (P-|O) %
            {~\mdf@frametitlefont{\theexercise}~};},%
    firstextra={%
            \node[titlered,xshift=1cm] at (P-|O) %
                    {~\mdf@frametitlefont{\theexercise}~};},
}
\makeatother

\newcommand{\SomeText}{%
    To calculate the horizontal position the kinematic differential
    equations are needed:
        \begin{align}
            \dot{n} &= u\cos\psi -v\sin\psi \\
            \dot{e} &= u\sin\psi + v\cos\psi
        \end{align}
    For small angles the following approximation can be used:
        \begin{align}
            \dot{n} &= u -v\delta_\psi \\
            \dot{e} &= u\delta_\psi + v
        \end{align}
}

\newenvironment{MyExercise}%
{\begin{mdframed}[style=exercisestyle]}{\end{mdframed}}

\begin{document}
   \begin{MyExercise}
       \SomeText

       \SomeText
   \end{MyExercise}

   \begin{MyExercise}
       \SomeText

       \SomeText

       \SomeText
   \end{MyExercise}
\end{document}

在此处输入图片描述

第三版本:一般的锻炼盒装tcolorbox

为了完成,我想添加一个解决方案tcolorbox包。使用最新版本,可以制作breakable带框的盒子。

此包提供了几个库来修改其默认行为,我们需要其中三个:

  1. breakable:允许框在页面之间自动换行。
  2. skins:在常规框上添加一些覆盖层。
  3. hooks:能够在我们的代码中使用一些特殊的钩子。

下一个代码包含一些注释来解释如何使用它。

\documentclass{article}
\usepackage{tikz}
\usepackage{etoolbox}
\usepackage{amsmath,amssymb}
\usepackage{lipsum}
\usepackage[skins,breakable,hooks]{tcolorbox}

\newcounter{myexercise}

% Definition of some styles and parameters for
% tcolorboxes
\tcbset{%
% Title boxes will be added with overlays
  fancytitle/.code={%
    \ifstrempty{#1}{}{\pgfkeysalso{%
        top=\baselineskip,
        overlay unbroken and first app = {%
        \node[rectangle, draw=red, fill=red,  
              anchor=west,font=\color{white}]
         at ([xshift=5mm]frame.north west)%
        {\strut#1};},
       enlarge top by = \baselineskip,
       enlarge top at break by= 0mm,
    }}},
% This is our boxes style
  fancybox/.style 2 args = {%
    breakable, enhanced, colback=blue!20,
    colframe=red, width=#1,
    fancytitle={#2},pad at break=2mm},
  fancybox/.default = {.5\textwidth}{}%
}

% This command creates a new environment called
% FancyBox which uses previously defined fancybox 
% style
\newtcolorbox{FancyBox}[2][.5\textwidth]{%
    fancybox={#1}{#2}}

% This command creates a new environment called
% FancyExercise which uses previously defined 
% fancybox style with an automatic enumerated
% title.
\newtcolorbox{FancyExercise}[1][.5\textwidth]{%
    code={\stepcounter{myexercise}},
     fancybox={#1}{Exercise~\arabic{myexercise}}}   


\newcommand{\SomeText}{To calculate the horizontal position the kinematic differential
        equations are needed:
        \begin{align}
            \dot{n} &= u\cos\psi -v\sin\psi \\
            \dot{e} &= u\sin\psi + v\cos\psi
        \end{align}
        For small angles the following approximation can be used:
        \begin{align}
            \dot{n} &= u -v\delta_\psi \\
            \dot{e} &= u\delta_\psi + v
        \end{align}
}

\begin{document}

\lipsum[1-2]

% Example of a FancyExercise
\begin{FancyExercise}[.75\textwidth]
        \SomeText
\end{FancyExercise}

% Example of a FancyExercise with default width
\begin{FancyExercise}
        \SomeText
\end{FancyExercise}

% Example of a FancyBox with a mandatory title
\begin{FancyBox}[\textwidth]{This is a tcolorbox}
        \SomeText
\end{FancyBox}

\end{document}

在此处输入图片描述

答案2

这是一个可能的解决方案;Fancybox环境有一个可选参数,用于框的宽度(默认= 0.5\textwidth),以及一个强制参数,用于框的标题(在没有给出标题的情况下会做出规定(参见下面的示例)):

\documentclass{article}
\usepackage[a5paper]{geometry}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{amsmath,amssymb}
\usetikzlibrary{calc}

\newenvironment{Fancybox}[2][.5\textwidth]
{%
  \ifstrempty{#2}%
    {}%
    {\mdfsetup{%
      frametitle={%
      \tikz[baseline=(current bounding box.east),outer sep=0pt]
      \node[draw=red,anchor=east,rectangle,fill=red,font=\color{white}]
        {\strut#2};}}%
    }%
  \mdfsetup{
    skipabove=\topskip,
    skipbelow=\topskip,
    innertopmargin=10pt,
    linecolor=red,%
    innerlinewidth=0.8pt,
    topline=true,
    backgroundcolor=blue!20,
    frametitleaboveskip=\dimexpr-\ht\strutbox\relax,
    roundcorner=5pt,
    singleextra={\path let \p1=(P), \p2=(O) in node[fill=red,font=\color{white}] at (P|-0,0.5*\y2+0.5*\y1) {$\clubsuit$};}
  }
  \begin{mdframed}[userdefinedwidth=#1]\relax%
}
{\end{mdframed}}

\begin{document}

\begin{Fancybox}{A fancy title}
        To calculate the horizontal position the kinematic differential
        equations are needed:
        \begin{align}
            \dot{n} &= u\cos\psi -v\sin\psi \\
            \dot{e} &= u\sin\psi + v\cos\psi
        \end{align}
        For small angles the following approximation can be used:
        \begin{align}
            \dot{n} &= u -v\delta_\psi \\
            \dot{e} &= u\delta_\psi + v
        \end{align}
\end{Fancybox}

\begin{Fancybox}[\textwidth]{Another fancy title}
        To calculate the horizontal position the kinematic differential
        equations are needed:
        \begin{align}
            \dot{n} &= u\cos\psi -v\sin\psi \\
            \dot{e} &= u\sin\psi + v\cos\psi
        \end{align}
        For small angles the following approximation can be used:
        \begin{align}
            \dot{n} &= u -v\delta_\psi \\
            \dot{e} &= u\delta_\psi + v
        \end{align}
        For small angles the following approximation can be used:
        \begin{align}
            \dot{n} &= u -v\delta_\psi \\
            \dot{e} &= u\delta_\psi + v
        \end{align}
        For small angles the following approximation can be used:
        \begin{align}
            \dot{n} &= u -v\delta_\psi \\
            \dot{e} &= u\delta_\psi + v
        \end{align}
\end{Fancybox}

\begin{Fancybox}[4cm]{}
        To calculate the horizontal position the kinematic differential equations are needed:
        \begin{align}
            \dot{n} &= u -v\delta_\psi \\
            \dot{e} &= u\delta_\psi + v
        \end{align}
\end{Fancybox}

\end{document}

在此处输入图片描述

由于问题中没有提供\clubsuit在环境跨越多页的情况下装饰的所需位置的信息,因此我只使用了 中的装饰singleextra,因此它只会在框架不包含分页符时出现(无论如何,这似乎是一个有效的选择)。如果需要其他行为,可以使用其他 ...extra 系列键轻松实现(有关mdframed详细信息,请参阅文档)。

有人要求添加一个计数器并有一个固定的“锻炼”标题;这使得事情变得更加简单:简单的修改就足够了;这个新代码还添加了 \allowdisplaybreaks(由马可·丹尼尔如果涉及显示数学,则有更好的断点):

\documentclass{article}
\usepackage[a5paper]{geometry}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{amsmath,amssymb}
\usetikzlibrary{calc}

\newcounter{exe}

\newenvironment{Fancybox}[1][.5\textwidth]
{%
    \stepcounter{exe}%
  \mdfsetup{%
      frametitle={%
      \tikz[baseline=(current bounding box.east),outer sep=0pt]
      \node[draw=red,anchor=east,rectangle,fill=red,font=\color{white}]
        {\strut Exercise~\theexe};},%
    skipabove=\topskip,
    skipbelow=\topskip,
    innertopmargin=10pt,
    linecolor=red,%
    innerlinewidth=0.8pt,
    topline=true,
    backgroundcolor=blue!20,
    frametitleaboveskip=\dimexpr-\ht\strutbox\relax,
    roundcorner=5pt,
    singleextra={\path let \p1=(P), \p2=(O) in node[fill=red,font=\color{white}] at (P|-0,0.5*\y2+0.5*\y1) {$\clubsuit$};}
  }
  \begin{mdframed}[userdefinedwidth=#1]\allowdisplaybreaks\relax%
}
{\end{mdframed}}

\begin{document}

\begin{Fancybox}
        To calculate the horizontal position the kinematic differential
        equations are needed:
        \begin{align}
            \dot{n} &= u\cos\psi -v\sin\psi \\
            \dot{e} &= u\sin\psi + v\cos\psi
        \end{align}
        For small angles the following approximation can be used:
        \begin{align}
            \dot{n} &= u -v\delta_\psi \\
            \dot{e} &= u\delta_\psi + v
        \end{align}
\end{Fancybox}

\begin{Fancybox}[\textwidth]
        To calculate the horizontal position the kinematic differential
        equations are needed:
        \begin{align}
            \dot{n} &= u\cos\psi -v\sin\psi \\
            \dot{e} &= u\sin\psi + v\cos\psi
        \end{align}
        For small angles the following approximation can be used:
        \begin{align}
            \dot{n} &= u -v\delta_\psi \\
            \dot{e} &= u\delta_\psi + v
        \end{align}
        For small angles the following approximation can be used:
        \begin{align}
            \dot{n} &= u -v\delta_\psi \\
            \dot{e} &= u\delta_\psi + v
        \end{align}
        For small angles the following approximation can be used:
        \begin{align}
            \dot{n} &= u -v\delta_\psi \\
            \dot{e} &= u\delta_\psi + v
        \end{align}
\end{Fancybox}

\begin{Fancybox}[4cm]
        To calculate the horizontal position the kinematic differential equations are needed:
        \begin{align}
            \dot{n} &= u -v\delta_\psi \\
            \dot{e} &= u\delta_\psi + v
        \end{align}
\end{Fancybox}

\end{document}

在此处输入图片描述

相关内容