如何在一个“主”环境中调用“子”环境?

如何在一个“主”环境中调用“子”环境?

在下面的代码中,我有 Gonzalo Medina 提供的两个精彩环境。

% Sources
%    1) http://tex.stackexchange.com/questions/33078/frame-with-only-crosses-in-two-opposite-corners/33087#33087
%    2) http://tex.stackexchange.com/questions/33110/how-to-use-named-variables-with-one-environment

\documentclass{article}
    \usepackage{keyval}
    \usepackage{xcolor}

    \makeatletter
% Length : this controls the length of the rules used (default = 1cm)
        \define@key{crossFrame}{length}{\def\mf@length{#1}}

% Horizontal distance between the rules and the text (default = 0.8cm)
        \define@key{crossFrame}{Hdist}{\def\mf@Hdist{#1}}
% Vertical distance between the rules and the text (default = 2ex)
        \define@key{crossFrame}{Vdist}{\def\mf@Vdist{#1}}

% Factor is a number controlling the point of intersection of each pair
% of rules used for the cross (default = 3)
        \define@key{crossFrame}{factor}{\def\mf@factor{#1}}

% Thickness of the rule (default = 0.4pt)
        \define@key{crossFrame}{thickness}{\def\mf@thickness{#1}}

% Color of the rule using the xcolor package (default = black)
        \define@key{crossFrame}{color}{\def\mf@color{#1}}

% Style : 2 corners or 4 (default = black)
        \define@key{crossFrame}{style}{\def\mf@style{#1}}

% We set the default values (except for Vdist which depends on current conditions)
        \setkeys{crossFrame}{
            length=1cm,
            thickness=0.4pt,
            Hdist=.8cm,
            factor=3,
            color=black,
            style=2
        }

        \newenvironment{crossFrameTwoCorners}[1][]{%
            \setkeys{crossFrame}{Vdist=2ex,#1}%
            \par\hfill\rlap{%
                \bgroup\color{\mf@color}%
                \hskip-\dimexpr\mf@length-\mf@Hdist\relax
                \rule{\mf@length}{\mf@thickness}%
                \hskip-\dimexpr\mf@length/\mf@factor\relax
                \rule[-\dimexpr\mf@length-\dimexpr\mf@length/\mf@factor\relax]{\mf@thickness}{\mf@length}%
                \egroup
            }%
            \vskip-\dimexpr\mf@length/\mf@factor+\dimexpr\mf@length/\mf@factor-\mf@Vdist\relax
        }{%
            \par\nobreak\offinterlineskip
            \vskip-\dimexpr\mf@length/\mf@factor+\dimexpr\mf@length/\mf@factor-\mf@Vdist\relax
            \noindent
            \hskip-\mf@Hdist\bgroup\color{\mf@color}%
            \rule{\mf@length}{\mf@thickness}\hskip-\dimexpr\mf@length-
            \dimexpr\mf@length/\mf@factor-\mf@thickness\relax
            \rule[-\dimexpr\mf@length/\mf@factor-\mf@thickness\relax]{\mf@thickness}{\mf@length}
            \egroup\par
        }

        \newenvironment{crossFrameFourCorners}[1][]{%
            \setkeys{crossFrame}{Vdist=2ex,#1}%
            \par\noindent\hskip-\mf@Hdist\bgroup\color{\mf@color}%
            \rule{\mf@length}{\mf@thickness}\hskip-\dimexpr\mf@length-\dimexpr\mf@length/\mf@factor-\mf@thickness\relax%
            \rule[-\dimexpr\mf@length-\dimexpr\mf@length/\mf@factor\relax]{\mf@thickness}{\mf@length}\egroup
            \hfill\rlap{%
                \bgroup\color{\mf@color}%
                \hskip-\dimexpr\mf@length-\mf@Hdist\relax\rule{\mf@length}{\mf@thickness}%
                \hskip-\dimexpr\mf@length/\mf@factor\relax\rule[-\dimexpr\mf@length-\dimexpr\mf@length/\mf@factor\relax]{\mf@thickness}{\mf@length}%
                \egroup
            }%
            \vskip-\dimexpr\mf@length/\mf@factor+\dimexpr\mf@length/\mf@factor-\mf@Vdist\relax%
        }{%
            \par\nobreak\offinterlineskip\vskip-\dimexpr\mf@length/\mf@factor+\dimexpr\mf@length/\mf@factor-\mf@Vdist\relax\noindent%
            \hskip-\mf@Hdist\bgroup\color{\mf@color}%
            \rule{\mf@length}{\mf@thickness}\hskip-\dimexpr\mf@length-\dimexpr\mf@length/\mf@factor-\mf@thickness\relax%
            \rule[-\dimexpr\mf@length/\mf@factor-\mf@thickness\relax]{\mf@thickness}{\mf@length}\egroup
            \hfill\rlap{%
                \bgroup\color{\mf@color}%
                \hskip-\dimexpr\mf@length-\mf@Hdist\relax\rule{\mf@length}{\mf@thickness}%
                \hskip-\dimexpr\mf@length/\mf@factor\relax\rule[-\dimexpr\mf@length/\mf@factor-\mf@thickness\relax]{\mf@thickness}{\mf@length}%
                \egroup
            }%
            \par
        }
    \makeatother

% Just to generate text for the example.
    \usepackage{lipsum}


\begin{document}

\section{''Two corners'' style}

\begin{crossFrameTwoCorners}[
    length=1.5cm,
    thickness=2pt,
    Hdist=1.3cm,
    color=blue,
    factor=2,
    Vdist=8ex
]
    \lipsum[1]
\end{crossFrameTwoCorners}


\newpage

\section{''Four corners'' style}

\begin{crossFrameFourCorners}[
    length=1.5cm,
    thickness=2pt,
    Hdist=1.3cm,
    color=blue,
    factor=2,
    Vdist=8ex
]
    \lipsum[1]
\end{crossFrameFourCorners}

\end{document}

我想构建一个名为的环境,如果键是 2,则crossFrame“调用” , 如果键是 4,则“调用”,否则发送错误。然后我可以使用:crossFrameTwoCornersstylecrossFrameFourCornersstyle

...

\begin{document}

\section{''Two corners'' style}

\begin{crossFrame}[
    style=2,
    length=1.5cm,
    thickness=2pt,
    Hdist=1.3cm,
    color=blue,
    factor=2,
    Vdist=8ex
]
    \lipsum[1]
\end{crossFrame}


\newpage

\section{''Four corners'' style}

\begin{crossFrame}[
    style=4,
    length=1.5cm,
    thickness=2pt,
    Hdist=1.3cm,
    color=blue,
    factor=2,
    Vdist=8ex
]
    \lipsum[1]
\end{crossFrame}

\end{document}

答案1

您可以使用\ifcase语句\mf@style并调用子环境。您需要将\setkeys部分从子环境移动到父环境,以便style在那里处理密钥。然后您不能再直接使用子环境。

要调用子环境,我建议使用普通形式\foobar而不是 LaTeX 形式,\begin{foobar}原因如下所述根据其他环境来定义环境:正确的方法是什么?。然后的结束代码crossframe将被临时指定为子环境的结束代码。这样可以为您节省第二条\ifcase语句。

\documentclass{article}
\usepackage{keyval}
\usepackage{xcolor}

\makeatletter

% Length : this controls the length of the rules used (default = 1cm)
\define@key{crossFrame}{length}{\def\mf@length{#1}}

% Horizontal distance between the rules and the text (default = 0.8cm)
\define@key{crossFrame}{Hdist}{\def\mf@Hdist{#1}}
% Vertical distance between the rules and the text (default = 2ex)
\define@key{crossFrame}{Vdist}{\def\mf@Vdist{#1}}

% Factor is a number controlling the point of intersection of each pair
% of rules used for the cross (default = 3)
\define@key{crossFrame}{factor}{\def\mf@factor{#1}}

% Thickness of the rule (default = 0.4pt)
\define@key{crossFrame}{thickness}{\def\mf@thickness{#1}}

% Color of the rule using the xcolor package (default = black)
\define@key{crossFrame}{color}{\def\mf@color{#1}}

% Style : 2 corners or 4 (default = black)
\define@key{crossFrame}{style}{\def\mf@style{#1}}

% We set the default values (except for Vdist which depends on current conditions)
\setkeys{crossFrame}{
    length=1cm,
    thickness=0.4pt,
    Hdist=.8cm,
    factor=3,
    color=black,
    style=2
}

\newenvironment{crossframe}[1][]{%
    \setkeys{crossFrame}{Vdist=2ex,#1}%
    \def\myerror{\PackageError{crossframe}{Wrong value of 'style'!}{}}%
    \ifcase\mf@style\relax
        \myerror% 0
    \or
        \myerror% 1
    \or% 2
        \let\endcrossframe\endcrossFrameTwoCorners
        \crossFrameTwoCorners
    \or
        \myerror% 3
    \or
        \let\endcrossframe\endcrossFrameFourCorners
        \crossFrameFourCorners
    \else
        \myerror% 3
    \fi
}{}

\newenvironment{crossFrameTwoCorners}{%
    \par\hfill\rlap{%
        \bgroup\color{\mf@color}%
        \hskip-\dimexpr\mf@length-\mf@Hdist\relax
        \rule{\mf@length}{\mf@thickness}%
        \hskip-\dimexpr\mf@length/\mf@factor\relax
        \rule[-\dimexpr\mf@length-\dimexpr\mf@length/\mf@factor\relax]{\mf@thickness}{\mf@length}%
        \egroup
    }%
    \vskip-\dimexpr\mf@length/\mf@factor+\dimexpr\mf@length/\mf@factor-\mf@Vdist\relax
}{%
    \par\nobreak\offinterlineskip
    \vskip-\dimexpr\mf@length/\mf@factor+\dimexpr\mf@length/\mf@factor-\mf@Vdist\relax
    \noindent
    \hskip-\mf@Hdist\bgroup\color{\mf@color}%
    \rule{\mf@length}{\mf@thickness}\hskip-\dimexpr\mf@length-
    \dimexpr\mf@length/\mf@factor-\mf@thickness\relax
    \rule[-\dimexpr\mf@length/\mf@factor-\mf@thickness\relax]{\mf@thickness}{\mf@length}
    \egroup\par
}

\newenvironment{crossFrameFourCorners}{%
    \par\noindent\hskip-\mf@Hdist\bgroup\color{\mf@color}%
    \rule{\mf@length}{\mf@thickness}\hskip-\dimexpr\mf@length-\dimexpr\mf@length/\mf@factor-\mf@thickness\relax%
    \rule[-\dimexpr\mf@length-\dimexpr\mf@length/\mf@factor\relax]{\mf@thickness}{\mf@length}\egroup
    \hfill\rlap{%
        \bgroup\color{\mf@color}%
        \hskip-\dimexpr\mf@length-\mf@Hdist\relax\rule{\mf@length}{\mf@thickness}%
        \hskip-\dimexpr\mf@length/\mf@factor\relax\rule[-\dimexpr\mf@length-\dimexpr\mf@length/\mf@factor\relax]{\mf@thickness}{\mf@length}%
        \egroup
    }%
    \vskip-\dimexpr\mf@length/\mf@factor+\dimexpr\mf@length/\mf@factor-\mf@Vdist\relax%
}{%
    \par\nobreak\offinterlineskip\vskip-\dimexpr\mf@length/\mf@factor+\dimexpr\mf@length/\mf@factor-\mf@Vdist\relax\noindent%
    \hskip-\mf@Hdist\bgroup\color{\mf@color}%
    \rule{\mf@length}{\mf@thickness}\hskip-\dimexpr\mf@length-\dimexpr\mf@length/\mf@factor-\mf@thickness\relax%
    \rule[-\dimexpr\mf@length/\mf@factor-\mf@thickness\relax]{\mf@thickness}{\mf@length}\egroup
    \hfill\rlap{%
        \bgroup\color{\mf@color}%
        \hskip-\dimexpr\mf@length-\mf@Hdist\relax\rule{\mf@length}{\mf@thickness}%
        \hskip-\dimexpr\mf@length/\mf@factor\relax\rule[-\dimexpr\mf@length/\mf@factor-\mf@thickness\relax]{\mf@thickness}{\mf@length}%
        \egroup
    }%
    \par
}
\makeatother

% Just to generate text for the example.
\usepackage{lipsum}


\begin{document}

\section{''Two corners'' style}

\begin{crossframe}[style=2]
    \lipsum[1]
\end{crossframe}


\newpage

\section{''Four corners'' style}

\begin{crossframe}[style=4]
    \lipsum[1]
\end{crossframe}

\end{document}

相关内容