Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

In the following code, I have two wonderfull environments given by 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}

I would lik to build one environment named crossFrame which "calls" crossFrameTwoCorners if the key style is 2, crossFrameFourCorners if the key style is 4, and sends an error otherwise. Then I could use :

...

\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}
share|improve this question
2  
Usually it is better to only a minimal working example (MWE) that illustrates your problem, not the whole bunch of code. For example, the exact implementation of the sub-environment is not really required here. –  Martin Scharrer Oct 31 '11 at 9:32
    
I've put the two environments because they need keys. –  projetmbc Oct 31 '11 at 10:05

1 Answer 1

up vote 6 down vote accepted

You can use a \ifcase statement on \mf@style and the call the child environments. You need to move the \setkeys part from the child to the parent environment, so that the style key is processed there. Then you can't use the child environments directly any longer.

To call the child environments I would recommend to use the plain form \foobar not the LaTeX form \begin{foobar} for the reason explained at Defining environments based on other ones: What's the right way?. Then the end-code of crossframe will be temporary assigned to be the end-code of the sub-environment. This saves you a second \ifcase statement there.

\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}
share|improve this answer
    
Thanks for the code and the comments. –  projetmbc Oct 31 '11 at 10:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.