1

I use two (and maybe more in the future) almost identical environments defined as

%%% REMARK ENVIROMENT 

\newcounter{remark}[chapter]
\def\theremark{\thesection\cyan{.\arabic{remark}}\blackink}
\newenvironment{remark}[2][]{\refstepcounter{remark}\par\medskip 
\noindent
\mdfsetup{
    frametitleaboveskip=-0.5\ht\strutbox,
    frametitlealignment=\raggedright }
\begin{mdframed}[hidealllines=true,backgroundcolor=black!10]
\cyan\hrulefill\blackink \\
\colorbox{cyan}{\textbf {\whiteink{\large{REMARK~\theremark}
\rule[-6pt] {0ex}{4ex} }\blackink #1}} 
\smallskip $\quad$ \textbf {#2} \\
\cyan\hrulefill\blackink
}{${}$\\\cyan\hrulefill\blackink\end{mdframed}}

%%% DEFINITION ENVIROMENT 

\newcounter{definition}[section]
\def\thedefinition{\thesection\cyan{.\arabic{definition}}\blackink}
\newenvironment{definition}[2][]{\refstepcounter{definition}\par\medskip  
\noindent
\mdfsetup{
    frametitleaboveskip=-0.5\ht\strutbox,
    frametitlealignment=\raggedright }
\begin{mdframed}[hidealllines=true,backgroundcolor=black!10]
\cyan\hrulefill\blackink \\
\colorbox{cyan}{\textbf {\whiteink{\large{DEFINITION~\thedefinition}   
\rule[-6pt]{0ex}{4ex} }\blackink #1}} \smallskip $\quad$ \textbf {#2} \\
\cyan\hrulefill\blackink
}{${}$\\\cyan\hrulefill\blackink\end{mdframed}}

(the only thing that changes is the label of the environment and (maybe) the color). Is it possible to combine them in a single environent (or to use them from another environment) and to select somehow the one i want to use (maybe via an additional parameter?) What if the environments differ in more than one features than their name? Thanks a lot.

3

It is always better to give a minimal working example when asking a question, as this normally helps explain what you want -- and it gives people some compliable code to start from.

The code below combines your two environments. Since you said that you might like the change the colour of the environments I have used \NewDocumentEnvironment from the xparse package to define the environments with two optional arguments. For example, you can now write:

\begin{definition}(blue!10)[really]{wonderful}
  Here is a really wonderful definition
\end{definition}

The first optional argument, which is surrounded by brackets, sets the background colour and the second optional argument is your previous optional argument.

Here is part of the output produced by the MWE code below:

enter image description here

Here is the full code:

\documentclass{book}
\usepackage{xcolor}
\usepackage{mdframed}
\usepackage{xparse}
\usepackage{etoolbox}
\mdfsetup{
    frametitleaboveskip=-0.5\ht\strutbox,
    frametitlealignment=\raggedright
}
\newcommand\blackink{\color{black}}
\newcommand\whiteink{\color{white}}
\newcommand\cyan{\color{cyan}}

\newcounter{remark}[chapter]
\newcounter{definition}[section]
\renewcommand\theremark{\thesection\cyan{.\arabic{remark}}\blackink}
% usage:
% \begin{MasterEnvironment}{remark/definition}{bg colour}{optional}{rmk/def}
\NewDocumentEnvironment{MasterEnvironment}{ m m m m }{%
  \refstepcounter{#1}\par\medskip
  \noindent
  \begin{mdframed}[hidealllines=true,backgroundcolor=#2]
 \cyan\hrulefill\blackink \\
  \colorbox{cyan}{\textbf{\whiteink{\large{\uppercase{#1}~\csuse{the#1}}
  \rule[-6pt] {0ex}{4ex} }\blackink #3}}
  \smallskip $\quad$ \textbf {#4} \\
  \cyan\hrulefill\blackink
}{${}$\\\cyan\hrulefill\blackink\end{mdframed}}

%%% REMARK ENVIROMENT
% usage: \begin{remark}(bg colour)[optional]{remark}...\end{remark}
\NewDocumentEnvironment{remark}{ D(){black!10} O{} m }%
  {\begin{MasterEnvironment}{remark}{#1}{#2}{#3}}
  {\end{MasterEnvironment}}

%%% DEFINITION ENVIROMENT
% usage: \begin{definition}(bg colour)[optional]{definition}...\end{definition}
\NewDocumentEnvironment{definition}{ D(){black!10} O{} m }%
  {\begin{MasterEnvironment}{definition}{#1}{#2}{#3}}
  {\end{MasterEnvironment}}

\begin{document}
\chapter{A walk in the woods}
\section{A nice section}
\begin{definition}{wonderful}
  Here is a wonderful definition
\end{definition}

\begin{definition}[really]{wonderful}
  Here is a really wonderful definition
\end{definition}

\begin{definition}(blue!10){wonderful}
  Here is a wonderful definition
\end{definition}

\begin{remark}{wonderful}
  Here is a wonderful remark
\end{remark}

\begin{remark}[really]{wonderful}
  Here is a really wonderful remark
\end{remark}

\begin{remark}(blue!10){wonderful}
  Here is a wonderful remark
\end{remark}

\end{document}

A few remarks:

  • The definition and remark environment both have two optional arguments and they pass everything that they are given to the MasterEnvironment, which has four mandatory arguments.
  • The first argument to MasterEnvironment sets both the name and counter to be used: that is, either definition or remark. Later \uppercase is used to put the name in all-caps.
  • I have used \csuse from the etoolbox package to print the counters as \csuse{the#1}. You could instead use \csname the#1\endcsname here, but I think that \csuse makes the code easier to read.
  • The definition of \theremark as \thesection\cyan{.\arabic{remark}}\blackink is a little strange on two counts. First, the remark counter is defined as \newcounter{remark}[chapter] so it is incremented in step with the chapter counter but \theremark uses the section counter instead. Secondly, the remark number is printed in cyan BUT when \theremark is used inside the definition environment it is printed inside a \colorbox with a cyan background, which means that you cannot see the \arabic{remark}. As it was written like this in the OP I left it like this because this might actually be what is wanted...but, perhaps not.
  • As \blackink, \whiteink and \cyan were not defined in the code posted with the question I have defined rough approximations.
  • I have moved the \mdfsetup{...} outside of the environment definition. Apart from this I have not changed the mdframed environment as I assume that this functions as expected

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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