1

It's well known that the LaTeX built-in command \newtheorem could generate a series of the theorem environments that are similar: they share the same theme of layout style but differ in their name and counter style etc.

I defined a colored text box, which is used to output a fancy style of class note, using TikZ recently. Although the specific style satisfied me myself, it also brought me some kind of inconvenience. Since I want to put definition, lemma, and conclusion, for example, into different but similar styles, I have to define such environments one by one, by using over ten lines of code.

MWE is here:

\documentclass{book}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{environ}
\usepackage{amsmath,mathrsfs,amsfonts}

\usetikzlibrary{shapes,decorations}
\definecolor{bule}{RGB}{18,29,57}
\definecolor{bablue}{RGB}{248,248,248}
\definecolor{main}{RGB}{127,191,51}
\definecolor{seco}{RGB}{0,145,215}
\definecolor{thid}{RGB}{180,27,131}

%define the style of tikz newthemsty
\tikzstyle{newthemsty} 
          =[draw=seco, fill=blue!10,very thick,rectangle,
           rounded corners, inner sep=10pt, inner ysep=20pt]
\tikzstyle{newthemstytitle}
          =[fill=seco, text=blue!10]

%%define the "newthem" environment
\newcounter{Newthem}[chapter]
\renewcommand{\theNewthem}{\thechapter.\arabic{Newthem}}
\NewEnviron{newthem}[1][{}]{%
\noindent\centering
\begin{tikzpicture}
\node[newthemsty] (box){
     \begin{minipage}{0.93\columnwidth}
        \sffamily\BODY
     \end{minipage}};
\node[newthemstytitle,right=10pt] at (box.north west){
     \bfseries\textsf{Theorem \stepcounter{Newthem}\theNewthem\; #1}};
\node[newthemstytitle, rounded corners] at (box.east) {$\clubsuit$};
\end{tikzpicture}
}[\par\vspace{.5\baselineskip}]

\begin{document}

\begin{newthem}
    test
\end{newthem}

\end{document}

And its output:

enter image description here

If I want a command that could define such an environment and I'm able to modify the color (of background and the edge), the font shape(italic or upright) and series(bold or not), the name in the label (say 'theorem' here) and the counter (within or without another counter), what should I do?

Any clues or hints would be appreciated.

  • 1
    Have a look at tcolorbox and mdframed before trying to reinvent the wheel: those packages provide ways to define new theorem styles in the way you like. – egreg Dec 21 '13 at 11:08
  • @egerg I do know these two packages provide interfaces to define fancy text box, but in fact, I need to use a \newtheorem-like command to define other things. Hence, how to create such a command is what I need. Could you help me? Vote up to your comment and thank you for the metaphor about wheel. :) – Ch'en Meng Dec 21 '13 at 11:12
  • Take a look at tcolorbox manual, section 8, p.155., for example. – karlkoeller Dec 21 '13 at 11:19
  • See also here: tex.stackexchange.com/questions/120454/… – Marco Daniel Dec 21 '13 at 13:36
  • What other things? If those packages could solve this problem, it isn't clear to me what kind of solution you need. That is, how do these other things differ from the problem statement you gave? How, that is, would a solution to this problem help with those if mdframed/tcolorbox can solve this problem but not those? I guess I just find the question confusing now, although the initial question itself seemed perfectly clear. – cfr Dec 22 '13 at 0:24
2

What you want to do is to wrap your code inside another macro that will take a name and some settings to control the color and style. Here I'm trying to roughly mimic \newtheorem and \theoremstyle.

\documentclass{book}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{environ}
\usepackage{amsmath,mathrsfs,amsfonts}
\usepackage{xparse}

\usetikzlibrary{shapes,decorations}
\definecolor{bule}{RGB}{18,29,57}
\definecolor{bablue}{RGB}{248,248,248}
\definecolor{main}{RGB}{127,191,51}
\definecolor{seco}{RGB}{0,145,215}
\definecolor{thid}{RGB}{180,27,131}


\newcommand{\newfancytheoremstyle}[5]{%
  \tikzset{#1/.style={draw=#3, fill=#2,very thick,rectangle,
      rounded corners, inner sep=10pt, inner ysep=20pt}}
  \tikzset{#1title/.style={fill=#3, text=#2}}
  \expandafter\def\csname #1headstyle\endcsname{#4}
  \expandafter\def\csname #1bodystyle\endcsname{#5}
}

\newfancytheoremstyle{fancythrm}{blue!10}{seco}{\bfseries\sffamily}{\sffamily}

\makeatletter
\DeclareDocumentCommand{\newfancytheorem}{ O{\@empty} m m m O{fancythrm} }{%
  % define the counter for the theorem
  \ifx#1\@empty
    \newcounter{#2}
  \else
    \newcounter{#2}[#1]
    \numberwithin{#2}{#1}
  \fi
  %% define the "newthem" environment
  \NewEnviron{#2}[1][{}]{%
    \noindent\centering
    \begin{tikzpicture}
      \node[#5] (box){
        \begin{minipage}{0.93\columnwidth}
          \csname #5bodystyle\endcsname \BODY~##1
        \end{minipage}};
      \node[#5title, right=10pt] at (box.north west){
        {\csname #5headstyle\endcsname #3 \stepcounter{#2}\csname the#2\endcsname\; ##1}};
      \node[#5title, rounded corners] at (box.east) {#4};
    \end{tikzpicture}
  }[\par\vspace{.5\baselineskip}]
}
\makeatother

% Define new styles
% \newfancytheoremstyle{<name>}{inner color}{outer color}{head style}{body style}
\newfancytheoremstyle{fancydef}{green!10}{green}{\itshape\sffamily}{\sffamily}

% Define some new environments
% \newfancytheorem[<number within>]{<name>}{<head>}{<symbol>}[<style>]
\newfancytheorem[chapter]{newthem}{Theorem}{$\clubsuit$}
\newfancytheorem[section]{newcor}{Corollary}{$\heartsuit$}

\newfancytheorem{newdef}{Definition}{$\spadesuit$}[fancydef]

\begin{document}

\begin{newthem}
    test
\end{newthem}

\begin{newcor}
  test
\end{newcor}

\begin{newdef}
  test
\end{newdef}

\end{document}

output

Use the \newfancytheoremstyle macro to define a style (colors and fonts) and \newfancytheorem to define a new environment. I'm using xparse to deal with the multiple option arguments. Note that the argument of the "inner" environment is referred to with ##1 instead of #1.

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.