使用 mdframed 和 TikZ 時,圓角在 \mdf@singleextra 中不起作用

使用 mdframed 和 TikZ 時,圓角在 \mdf@singleextra 中不起作用

我正在使用基於 中給出的離題範例的mdframed套件(以及)來創建新的框架環境。我的程式碼如下:TikZmdframed-example-texsx.tex

\documentclass{scrartcl}

\usepackage{times}

\usepackage{lipsum}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{tikz}
   \usetikzlibrary{calc,arrows,shadows}
\usepackage[framemethod=tikz]{mdframed}


\tikzset{
    title/.style={
      fill=white,
      font=\normalfont,
      text=black,
      anchor=base west,
    },
    contour/.style = {
      line width = 0.6pt,
      draw = black,
      rounded corners = 2ex,
    },
    fakeshadow/.style = {
      line width = 4.5pt,
      draw = white,
    },
}

\newcommand{\definitiontitle}{
  {\scshape \bfseries \Large Definition}
}

\mdfdefinestyle{definition}{%
    singleextra={%
          %% Store (O) in \p1, store (P) in \p2. Now \p1=(\x1,\y1) and \p2=(\x2,\y2). From that, define (Q) = (\x1,\y2).
          \path let \p1=(O), \p2=(P) in (\x1,\y2) coordinate (Q);
          \path let \p1=(O), \p2=(P) in (\x2,\y1) coordinate (R);
          \path let \p1=(O), \p2=(Q) in (\x1,{(\y1+\y2)/2}) coordinate (M);
          \path[contour] (M) |- (P) |- (O) -- (M);
          \node[title, anchor=west, xshift=18pt - 5pt] at (Q) {\definitiontitle};
    },
    firstextra={%
          \path let \p1=(O), \p2=(P) in (\x1,\y2) coordinate (Q);
          \path let \p1=(O), \p2=(P) in (\x2,\y1) coordinate (R);
          \path[contour] (O) -- (Q) -- (P) -- (R);
          \node[title, anchor=west, xshift=18pt - 5pt] at (Q) {\definitiontitle};
          \path[fakeshadow] ($(O)+(1pt,-1.5pt)$) -- ($(R)+(-1pt,-1.5pt)$);  %% Hide the bottom shadow
    },
    secondextra={%
          \path let \p1=(O), \p2=(P) in (\x1,\y2) coordinate (Q);
          \path let \p1=(O), \p2=(P) in (\x2,\y1) coordinate (R);
          \path[contour] (Q) -- (O) -- (R) -- (P);
    },
    middleextra={%
          \path let \p1=(O), \p2=(P) in (\x1,\y2) coordinate (Q);
          \path let \p1=(O), \p2=(P) in (\x2,\y1) coordinate (R);
          \path[contour] (O) -- (Q);
          \path[contour] (P) -- (R);
          \path[fakeshadow] ($(O)+(1pt,-1.5pt)$) -- ($(R)+(-1pt,-1.5pt)$);  %% Hide the bottom shadow
    },
    align=center,
    backgroundcolor=yellow,
    userdefinedwidth=.9\textwidth,
    middlelinewidth=1.7em,middlelinecolor=white,
    hidealllines=true,topline=true,
    innertopmargin=6pt,
    innerbottommargin=18pt,
    innerleftmargin=18pt,
    innerrightmargin=18pt,
    splitbottomskip=8pt,
    splittopskip=16pt,
    roundcorner=2ex,
    shadow=true,
    shadowsize=5,
    shadowcolor=black!40,
    %% Experimental
    needspace=3em,
    ignorelastdescenders=true,
}



\begin{document}

\lipsum[3]

\vspace{1\baselineskip}
\begin{mdframed}[style=definition]
    \lipsum[1]
\end{mdframed}

\vspace{1\baselineskip}
\lipsum[3]

\vspace{1\baselineskip}
\begin{mdframed}[style=definition]
    \lipsum[1-2]
\end{mdframed}

\vspace{1\baselineskip}
\lipsum[3]

\vspace{1\baselineskip}
\begin{mdframed}[style=definition]
    \lipsum[1-8]
\end{mdframed}

\end{document}


我目前正在使用 TikZ 放置自訂標題並繪製框架。這是必需的,以便使用適當的middlelinewidth補償標題高度,以便 mdframed 知道在哪裡分割。然而,我也使用圓角,這就是整體問題發生的地方。

正如馬可·丹尼爾 (Marco Daniel) 在這個帖子,使用程式碼很重要

    hidealllines=true,topline=true,


然而,hidealllines=true在 中使用會產生意想不到的效果\mdf@singleextra,即圓角消失,取而代之的是呈現一個完整的框框。當框架被分割時(或hidealllines=true不使用框架,但無法正確計算高度時),這種情況不會發生,如輸出所示:

輸出

知道為什麼這種情況只發生在\mdf@singleextra以及如何解決它嗎?

答案1

如果設定該選項,則會出現背景繪製時沒有任何圓角的問題topline=true。不過你可以破解測試:

\makeatletter
\let\mdf@putbox@single@orig\mdf@putbox@single
\mdfapptodefinestyle{definition}{%
  settings={%
        \def\mdf@putbox@single{%
                   \let\mdf@test@t\@gobbletwo
                   \let\mdf@test@noline\@firstoftwo
                 \mdf@putbox@single@orig
            }%
   }%
}
\makeatother

破解後,您必須singlextra透過以下行擴展該選項:

\path[draw=white,line width=1.7em,overlay] (O|-P) -- (P);

繪製標題的白色背景。

第二頁a的奇數角無法再現。

這裡的輸出:

在此輸入影像描述

這裡是完整的程式碼:

\documentclass{scrartcl}

\usepackage{times}

\usepackage{lipsum}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{tikz}
   \usetikzlibrary{calc,arrows,shadows}
\usepackage[framemethod=tikz]{mdframed}


\tikzset{
    title/.style={
      fill=white,
      font=\normalfont,
      text=black,
      anchor=base west,
    },
    contour/.style = {
      line width = 0.6pt,
      draw = black,
      rounded corners = 2ex,
    },
    fakeshadow/.style = {
      line width = 4.5pt,
      draw = white,
    },
}

\newcommand{\definitiontitle}{
  {\scshape \bfseries \Large Definition}
}

\mdfdefinestyle{definition}{%
    singleextra={%
          %% Store (O) in \p1, store (P) in \p2. Now \p1=(\x1,\y1) and \p2=(\x2,\y2). From that, define (Q) = (\x1,\y2).
          \path let \p1=(O), \p2=(P) in (\x1,\y2) coordinate (Q);
          \path let \p1=(O), \p2=(P) in (\x2,\y1) coordinate (R);
          \path let \p1=(O), \p2=(Q) in (\x1,{(\y1+\y2)/2}) coordinate (M);
         \path[draw=white,line width=1.7em,overlay] (O|-P) -- (P);
          \path[contour,] (M) |- (P) |- (O) -- (M);
          \node[title, anchor=west, xshift=18pt - 5pt] at (Q) {\definitiontitle};
    },
    firstextra={%
          \path let \p1=(O), \p2=(P) in (\x1,\y2) coordinate (Q);
          \path let \p1=(O), \p2=(P) in (\x2,\y1) coordinate (R);
          \path[contour] (O) -- (Q) -- (P) -- (R);
          \node[title, anchor=west, xshift=18pt - 5pt] at (Q) {\definitiontitle};
          \path[fakeshadow] ($(O)+(1pt,-1.5pt)$) -- ($(R)+(-1pt,-1.5pt)$);  %% Hide the bottom shadow
    },
    secondextra={%
          \path let \p1=(O), \p2=(P) in (\x1,\y2) coordinate (Q);
          \path let \p1=(O), \p2=(P) in (\x2,\y1) coordinate (R);
          \path[contour] (Q) -- (O) -- (R) -- (P);
    },
    middleextra={%
          \path let \p1=(O), \p2=(P) in (\x1,\y2) coordinate (Q);
          \path let \p1=(O), \p2=(P) in (\x2,\y1) coordinate (R);
          \path[contour] (O) -- (Q);
          \path[contour] (P) -- (R);
          \path[fakeshadow] ($(O)+(1pt,-1.5pt)$) -- ($(R)+(-1pt,-1.5pt)$);  %% Hide the bottom shadow
    },
    align=center,
    backgroundcolor=yellow,
    userdefinedwidth=.9\textwidth,
    middlelinewidth=1.7em,middlelinecolor=white,
    hidealllines=true,topline=true,
    innertopmargin=6pt,
    innerbottommargin=18pt,
    innerleftmargin=18pt,
    innerrightmargin=18pt,
    splitbottomskip=8pt,
    splittopskip=16pt,
    roundcorner=2ex,
%    shadow=true,
    shadowsize=5,
    shadowcolor=black!40,
    %% Experimental
    needspace=3em,
    ignorelastdescenders=true,
}




\makeatletter
\let\mdf@putbox@single@orig\mdf@putbox@single
\mdfapptodefinestyle{definition}{%
  settings={%
        \def\mdf@putbox@single{%
                   \let\mdf@test@t\@gobbletwo
                   \let\mdf@test@noline\@firstoftwo
                 \mdf@putbox@single@orig
            }%
   }%
}
\makeatother


\begin{document}

\lipsum[3]

\vspace{1\baselineskip}
\begin{mdframed}[style=definition]
    \lipsum[1]
\end{mdframed}

\vspace{1\baselineskip}
\lipsum[3]

\vspace{1\baselineskip}
\begin{mdframed}[style=definition]
    \lipsum[1-2]
\end{mdframed}

\vspace{1\baselineskip}
\lipsum[3]

\vspace{1\baselineskip}
\begin{mdframed}[style=definition]
    \lipsum[1-8]
\end{mdframed}

\end{document}

相關內容