3

I'm trying to create a theoremstyle whith a shaded background. As I already have a working preamble I'd like to change as less as possible. Previously I defined an own theoremstyle 'selfdefined' which I used for theorems and such. However as this is not shading my theorems it's not an option anymore.

Now I tried to define a new theoremstyle 'shaded' which is supposed to do this. Unfortunately it's not working the way I want it to work. Instead of using the newly defined theoremstyle it just uses some kind of default theoremstyle. I hope somebody can help me to fix this (with as few changes as possible and detailed explanations, as I'm pretty much a beginner when it comes to latex).

I'm aware that this is not really a 'minimum working' example, I hope it's still alright.

\documentclass[a4paper,12pt]{scrartcl}

\usepackage[utf8]{inputenc}                     % input encoding
\usepackage[T1]{fontenc}                        % use T1 fonts for font encoding
\usepackage{amsfonts}                           % math font
\usepackage{color}
\usepackage{dsfont}                             % math font for number spaces etc.
\usepackage{helvet}                             % sans serif font
\usepackage{inconsolata}
\usepackage{tikz}                               % for drawings.
\usepackage{latexsym}
\usepackage{stmaryrd}
\usepackage{mathtools}
\usepackage{parskip}                            % vertical space instead of indentation 
\usepackage[colorlinks=true,linkcolor=blue]{hyperref} 
\usepackage{thmtools}
\usepackage{mdframed}                               % shaded environments
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsthm}

\newtheoremstyle{selfdefined}
{12pt}% Space above
{12pt}% Space below
{}% Body font \itshape =italian style 
{}% Indent amount
{\bfseries}% Theorem head font
{}% Punctuation after theorem heading
{\newline}% Space after theorem heading, 0.5em => in gleicher zeile weiter
{}% Theorem head spec (can be left empty, meaning ‘normal’)

\definecolor{shadecolor}{gray}{0.65}
\declaretheoremstyle[
headfont=\normalfont\bfseries,
notefont=\mdseries, notebraces={(}{)},
bodyfont=\normalfont,
postheadspace=0.5em,
mdframed={
  skipabove=\topsep,
  skipbelow=\topsep,
  hidealllines=true,
  backgroundcolor={shadecolor},
  innerleftmargin=0pt,
  innerrightmargin=0pt}
]{shaded}

%\swapnumbers
\theoremstyle{shaded}
\newtheorem{definition}{Definition}[section]
\newtheorem{theorem}[definition]{Theorem}
\newtheorem{conjecture}[definition]{Conjecture}
\newtheorem{corollary}[definition]{Corollary}
\newtheorem{lemma}[definition]{Lemma}
\newtheorem{example}[definition]{Example}
\newtheorem*{algorithm}{Algorithm}

\begin{document}
\begin{definition}[\textbf{Definition 1}]
This is supposed to be a definition in shaded style.
\end{definition}

\begin{theorem}
This is supposed to be a theorem in shaded style.
\end{theorem}
\end{document}
3

There are several problems; the first two are the main ones:

  • You need to load thmtools after amsthm.

  • Since you used \declaretheoremstyle for the shaded style, you need to define your structures using \declaretheorem instead of \newtheorem, and pass the shaded style as an option to the style key instead of using \theoremstyle.

  • Load hyperref in the last place (except some exceptions which can be found elsewhere here).

  • Since you are using a KOMA class, instead of loading the parskip package you should use the parskip class option; in my example I used parskip=half+ but you can use the desired value (refer to the KOMA users guide to see available options).

The corrected code (adjust the settings according to your needs):

\documentclass[a4paper,12pt,parskip=half+]{scrartcl}
\usepackage[utf8]{inputenc}                     % input encoding
\usepackage[T1]{fontenc}                        % use T1 fonts for font encoding
\usepackage{amsfonts}                           % math font
\usepackage{color}
\usepackage{dsfont}                             % math font for number spaces etc.
\usepackage{helvet}                             % sans serif font
\usepackage{inconsolata}
\usepackage{tikz}                               % for drawings.
\usepackage{latexsym}
\usepackage{stmaryrd}
\usepackage{mathtools}
\usepackage{mdframed}                               % shaded environments
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage[colorlinks=true,linkcolor=blue]{hyperref} 

\newtheoremstyle{selfdefined}
{12pt}% Space above
{12pt}% Space below
{}% Body font \itshape =italian style 
{}% Indent amount
{\bfseries}% Theorem head font
{}% Punctuation after theorem heading
{\newline}% Space after theorem heading, 0.5em => in gleicher zeile weiter
{}% Theorem head spec (can be left empty, meaning ‘normal’)

\definecolor{shadecolor}{gray}{0.65}
\declaretheoremstyle[
headfont=\normalfont\bfseries,
notefont=\mdseries, notebraces={(}{)},
bodyfont=\normalfont,
postheadspace=0.5em,
spaceabove=6pt,
mdframed={
  skipabove=8pt,
  skipbelow=6pt,
  hidealllines=true,
  backgroundcolor={shadecolor},
  innerleftmargin=0pt,
  innerrightmargin=0pt}
]{shaded}

\declaretheorem[style=shaded,within=section]{definition}
\declaretheorem[style=shaded,sibling=definition]{theorem}
\declaretheorem[style=shaded,sibling=definition]{corollary}
\declaretheorem[style=shaded,sibling=definition]{conjecture}
\declaretheorem[style=shaded,sibling=definition]{lemma}
\declaretheorem[style=shaded,sibling=definition]{example}
\declaretheorem[style=shaded,numbered=no]{algorithm}

\begin{document}

\section{A test section}
\begin{definition}[\textbf{Definition 1}]
This is supposed a definition in shaded style.
\end{definition}
Some test text
\begin{theorem}
This is a theorem in shaded style.
\end{theorem}

\end{document}

The result:

enter image description here

The result:

  • Nice, thank you very much, thats exactly what I was looking for. Unfortunately a new problem just arose. Oftentimes the boxes don't really use up the space. So before a pagebreak, theres a space of up to 5-6 lines which is not used by the box, even though the 'insides' would neatly fit in there. Is there a way to fix this? – sblasher Aug 5 '15 at 21:43
  • @sblasher You're welcome. Since some code is needed for tests, I think this new problem deserves a new question; the best course of action now would be to open a follow-up question providing a MWE allowing us to reproduce the problem. – Gonzalo Medina Aug 5 '15 at 22:19

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.