mdframed:如何设计“锻炼”框

mdframed:如何设计“锻炼”框

对于定理和类似的东西,我使用mdframed包。对于定理、定义,我使用标准设计,而对于例子和练习,我更喜欢使用更奇特的设计……

到目前为止我成功做出以下解决方案:

\documentclass[12pt,twoside]{book}
    \usepackage[T1]{fontenc}
    \usepackage[utf8]{inputenc}
    \usepackage{lipsum}
    \usepackage{showframe}

\usepackage[framemethod=tikz]{mdframed}
\usetikzlibrary{calc, positioning, shapes.geometric}

\tikzset{
    node distance= 0mm and 3mm,
ws/.style={align=#1,
           font=\large\bfseries\sffamily, 
           draw, thin, fill=white,
           overlay,
           }
        }

\makeatletter
\mdfdefinestyle{example}%
{%
    hidealllines=true, 
    skipabove=2\baselineskip, skipbelow=-1ex,
    innertopmargin=0pt, innerbottommargin=0pt,%
    middlelinewidth=5pt, linecolor=red,%
    fontcolor=teal, font=\small\sffamily,%
%
settings={%\global\refstepcounter{example}
        \ifodd\value{page}
    \boolfalse{mdf@leftline}
    \booltrue{mdf@rightline}
        \else
    \booltrue{mdf@leftline}
    \boolfalse{mdf@rightline}
        \fi},
    innerrightmargin=\ifodd\value{page}0.5em\else 0.0em\fi, %rightmargin=7em,%
    innerleftmargin =\ifodd\value{page}0.0em\else 0.5em\fi, %leftmargin =7em,%
singleextra={
        \ifodd\value{page}
\node[ws=left,below right=of P] {\theexample};
        \else
\node[ws=right,below left=of O |- P]  {\theexample};
        \fi
             },%
}% end of mdfdefinestyle
\makeatother

\mdtheorem[style=example,
           ]{example}{Example}[chapter]

\mdtheorem[style=exercise,
           ]{exercise}{Exercise}[chapter]

\begin{document}
\chapter{test}

\begin{mdframed}[style=example]
    This is produced by \verb+\begin{mdframed}[style=example] ... \end{mdframed}+
\end{mdframed}
xxxxxxxxx 
\begin{example}
    This is produced by \verb+\begin{example} ... \end{example}+
\end{example}

\newpage
\lipsum[2]
\begin{mdframed}[style=example]
    \lipsum[2]
\end{mdframed}

\begin{example}
    \lipsum[2]
\end{example}

\end{document}

在此处输入图片描述

我喜欢使用“示例”而不是示例文本的顶部,而是在文本右侧带有数字的框中(如果页面是偶数,则在左侧)。这可能吗?

也欢迎对我的代码进行任何改进!

附录: 同时,我放弃了使用 来管理解决方案,mdframed而是使用 来定义等效环境tcolorbox(如早期评论中所建议的那样)。使用 解决方案如下:

\documentclass[12pt,twoside]{book}
    \usepackage[T1]{fontenc}
    \usepackage[utf8]{inputenc}
    \usepackage[many]{tcolorbox}
\usetikzlibrary{positioning}
    \usepackage{etoolbox}

    \usepackage{lipsum}
    \usepackage{showframe}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%  EXERCISES                                                   %
%%==============================================================%
\newcounter{task}
\AtBeginEnvironment{task}{\linespread{.9}\selectfont}
%---------------------------------------------------------------%
\newtcolorbox[use counter=task,
              number within=section,
              ]{task}%
   {enhanced jigsaw,
    breakable,
    toggle left and right,
    boxrule=0mm,
    colback=white, colframe=red,
    top=0mm,bottom=1mm,left=1mm,right=1mm,
    arc=0mm,
    borderline={0.5pt}{0pt}{red, sharp corners},
    fontupper=\normalsize\sffamily, fontlower=\normalsize\sffamily,
    rightrule=1mm,
overlay={\renewcommand\thetcbcounter{\thesection-\arabic{\tcbcounter}}%
\tcbifoddpage{\node[font=\large\sffamily, text=red, inner sep=0mm,
                    below right=0mm and \marginparsep] at (frame.north east) {Vaja
                    \thetcbcounter};}
             {\node[font=\large\sffamily, text=red, inner sep=0mm,
                    below left=0mm and \marginparsep]  at (frame.north west) {Vaja
                    \thetcbcounter};}
        }%end of overlay
    }% end of task

\begin{document}
\chapter{Chapter title}
\section{Test of "task" environment defined\newline 
         with use of "tcolorbox" package}
I looking for equivalent solution with package \verb+mdframed+, so far my attempts was unsuccessful.
\begin{task}
    This is produced by \verb+\begin{task}{}{} ... \end{task}+ defined with \verb+tcolorbox+
\end{task}
\lipsum[2]
\begin{task}
    \lipsum[1-2]
\end{task}
\end{document}

这使:

在此处输入图片描述

结果几乎令人满意......在其中我无法识别“Vaja ...”是否在第二页。如果是,我想添加(在第二行)“nadaljevanje”(继续),但是我把这个留到我完成教科书的时候,我会使用这个环境。

现在我正在测试 G. Bay 解决方案。按原样,它起作用了,不幸的是没有编号,正如我所料,但是,将它集成到我的mdframed基于集合的定理、定义等中时,我仍然遇到问题。

在最终的解决方案中,我喜欢获得与其他mdframed定理中使用的类似的解决方案,例如在定义中:

    \begin{definition}{Informacija}{info}
    \index{informacije!definicija}
Informacija je enaka nezanesljivosti, ki jo nastop dogodka odstrani.
    \end{definition}
Iz definicije \ref{def:info} sledi:

所以我仍然在寻找任何建议。

答案1

如果我理解正确的话,这或多或少就是你所想的:

奇数/偶数页的标题示例

基本上所做的就是添加以下内容:

frametitle={\ifodd\value{page} \rlap{\hspace*{15pt} Example}  \else \llap{Example \hspace*{15pt}}\fi},, %This is used to overright the numbering rightafter the "Example" in the original frame title (which was above the text)
frametitlealignment={\ifodd\value{page} \raggedleft \else \raggedright\fi},
  • “示例”一词不再像以前一样位于标题正上方,而是位于旁边。
  • 上面没有编号。
  • “Example” 一词在奇数页/偶数页上确实从左/右发生变化。
  • 即使 mdframed 跨越分页符,“Example”一词也能正确定位。

请注意,example环境仍然使用带有文本上方编号的框架标题,这与 不同style=example。可能需要进行额外的调整来更改文本颜色/字体或将其框起来等。

完整代码:

\documentclass[12pt,twoside]{book}
    \usepackage[T1]{fontenc}
    \usepackage[utf8]{inputenc}
    \usepackage{lipsum}
    \usepackage{showframe}

\usepackage[framemethod=tikz]{mdframed}
\usetikzlibrary{calc, positioning, shapes.geometric}

\tikzset{
    node distance= 0mm and 3mm,
ws/.style={align=#1,
           font=\large\bfseries\sffamily, 
           draw, thin, fill=white,
           overlay,
           }
        }

\makeatletter
\mdfdefinestyle{example}%
{%
    hidealllines=true, 
    skipabove=2\baselineskip, skipbelow=-1ex,
    innertopmargin=0pt, innerbottommargin=0pt,%
    middlelinewidth=5pt, linecolor=red,%
    fontcolor=teal, font=\small\sffamily,%
    frametitle={\ifodd\value{page} \rlap{\hspace*{15pt} Example}  \else \llap{Example \hspace*{15pt}}\fi},, %This is used to overright the numbering rightafter the "Example" in the original frame title (which was above the text)
    frametitlealignment={\ifodd\value{page} \raggedleft \else \raggedright\fi},
%
%
settings={%\global\refstepcounter{example}
        \ifodd\value{page}
    \boolfalse{mdf@leftline}
    \booltrue{mdf@rightline}
        \else
    \booltrue{mdf@leftline}
    \boolfalse{mdf@rightline}
        \fi},
    innerrightmargin=\ifodd\value{page}0.5em\else 0.0em\fi, %rightmargin=7em,%
    innerleftmargin =\ifodd\value{page}0.0em\else 0.5em\fi, %leftmargin =7em,%
singleextra={
%        \ifodd\value{page}
%\node[ws=left,below right=of P] {Example}; %These were the numbers beside them
%        \else
%\node[ws=right,below left=of O |- P]  {Example};
%        \fi
             },%
}% end of mdfdefinestyle
\makeatother

\mdtheorem[style=example,
           ]{example}{Example}[chapter]

\mdtheorem[style=exercise,
           ]{exercise}{Exercise}[chapter]

\begin{document}
\chapter{test}

\begin{mdframed}[style=example]
    This is produced by \verb+\begin{mdframed}[style=example] ... \end{mdframed}+
\end{mdframed}
xxxxxxxxx 
\begin{example}
    This is produced by \verb+\begin{example} ... \end{example}+
\end{example}

\newpage
\lipsum[2]
\begin{mdframed}[style=example]
        \lipsum[2]
\end{mdframed}

\begin{mdframed}[style=example]
\lipsum[2]
\lipsum[2]
\lipsum[2]
\lipsum[2]

\end{mdframed}

\end{document}

编辑:添加一些细节后,我修改了代码以满足:

  • 在单词“example”后进行编号
  • 根据偶数/奇数页向左/向右。
  • 当框架分割到另一页时显示“继续”文本。

为此,我使用了firstextra(向所有帧添加“示例编号”)和secondextra(仅向分割帧的第一部分添加“继续”)。请阅读第 7 节以了解更多Hooks and Bools信息mdframed如果您希望在分割帧的最后一部分添加“继续”,请检查这个问题

继续下页...

\documentclass[12pt,twoside]{book}
    \usepackage[T1]{fontenc}
    \usepackage[utf8]{inputenc}
    \usepackage{lipsum}
    \usepackage{showframe}
\usepackage[framemethod=tikz]{mdframed}
\usetikzlibrary{calc, positioning, shapes.geometric}

\tikzset{
    node distance= 0mm and 3mm,
ws/.style={align=#1,
           font=\large\bfseries\sffamily, 
           draw, thin, fill=white,
           overlay,
           }
        }
%\newcounter{example} maybe this is unnecessary
\makeatletter
\mdfdefinestyle{example}%
{%
    hidealllines=true,    
    skipabove=2\baselineskip, skipbelow=-1ex,
    innertopmargin=0pt, innerbottommargin=0pt,%
    middlelinewidth=5pt, linecolor=red,%
    fontcolor=teal, font=\small\sffamily,%
%
settings={\global\refstepcounter{example} %this was commented, it is now active
        \ifodd\value{page}
    \boolfalse{mdf@leftline}
    \booltrue{mdf@rightline}
        \else
    \booltrue{mdf@leftline}
    \boolfalse{mdf@rightline}
        \fi},
    innerrightmargin=\ifodd\value{page}0.5em\else 0.0em\fi, %rightmargin=7em,%
    innerleftmargin =\ifodd\value{page}0.0em\else 0.5em\fi, %leftmargin =7em,%
    %
singleextra={
    \ifodd\value{page}
    \node[ws=left,draw=none,below right=of P] { Example \theexample };
            \else
    \node[ws=right,draw=none,below left=of O |- P]  {Example \theexample };
            \fi
                 },%
firstextra={
\ifodd\value{page}
\node[ws=left,draw=none,below right=of P] { Example \theexample };
\node[ws=left,yshift=-2em,below right=of P] { continue \ldots };
        \else
\node[ws=right,draw=none,below left=of O |- P]  {Example \theexample };
\node[ws=right,draw=none,yshift=-2em,below left=of O |- P]  {continue \ldots };
        \fi
             },%
}% end of mdfdefinestyle
\makeatother

\mdtheorem[style=example,
           ]{example}{Example}[chapter]

\mdtheorem[style=exercise,
           ]{exercise}{Exercise}[chapter]

\begin{document}
\chapter{test}

\begin{mdframed}[style=example]
    This is produced by \verb+\begin{mdframed}[style=example] \end{mdframed}+
\end{mdframed}
xxxxxxxxx 
\begin{mdframed}[style=example]
    This is produced by \verb+\begin{mdframed}[style=example] \end{mdframed}+
\end{mdframed}
\newpage
\lipsum[2]
\begin{mdframed}[style=example]
        \lipsum[2]
\end{mdframed}

\begin{mdframed}[style=example]
\lipsum[2]
\lipsum[2]
\lipsum[2]
\lipsum[2]
\end{mdframed}
\end{document}

相关内容