1

I am new to latex and learning more tools on the fly, by using templates and playing around with parameters and arguments.

So I tried the following tikz construction for boxing a theorem

\newcounter{theorem}[chapter] \setcounter{theorem}{0}
\renewcommand{\thetheorem}{\arabic{chapter}.\arabic{theorem}}
\newenvironment{theorem}[2][]{%
\refstepcounter{theorem}%
\ifstrempty{#1}%
{\mdfsetup{%
frametitle={%
\tikz[baseline=(current bounding box.east),outer sep=.5pt]
\node()[anchor=east,rectangle,fill=blue!20]
{\strut Theorem~\thetheorem};}}
}%
{\mdfsetup{%
frametitle={%
\tikz[baseline=(current bounding box.east),outer sep=.5pt]
\node()[anchor=east,rectangle,fill=red!20]
{\strut Theorem~\thetheorem:~#1};}}%
}%
\mdfsetup{innertopmargin=5pt,linecolor=blue!20,%
linewidth=2pt,topline=true,%
frametitleaboveskip=\dimexpr-\ht\strutbox\relax
}
\begin{mdframed}[]\relax%
\label{#2}}{\end{mdframed}}

What this does is block out the first couple of letters in the theorem statement. Moreover, it often splits across different pages in an ugly way. I tried tweeking around with the parameters and margins, etc. but to no avail.

Is there a way to fix this?

Here's a sample document you can copy and compile for yourself to see the error first hand.

\documentclass[15pt]{book}
\usepackage[framemethod=TikZ]{mdframed}
\usepackage{,tikz-           cd,amssymb,amsmath,amsthm,amsfonts,sectsty,url,graphicx,MnSymbol,algorithm,listings,color,appendix,mathrsfs,makeidx}
\usepackage[letterpaper,hmargin=1.0in,vmargin=1.0in]{geometry}
\usepackage[Bjornstrup]{fncychap}
\pagestyle{plain}

\makeindex

\newcounter{theorem}[chapter] \setcounter{theorem}{0}
\renewcommand{\thetheorem}{\arabic{chapter}.\arabic{theorem}}
\newenvironment{theorem}[2][]{%
\refstepcounter{theorem}%
\ifstrempty{#1}%
{\mdfsetup{%
frametitle={%
\tikz[baseline=(current bounding box.east),outer sep=.5pt]
\node()[anchor=east,rectangle,fill=blue!20]
{\strut Theorem~\thetheorem};}}
}%
{\mdfsetup{%
frametitle={%
\tikz[baseline=(current bounding box.east),outer sep=.5pt]
\node()[anchor=east,rectangle,fill=red!20]
{\strut Theorem~\thetheorem:~#1};}}%
}%
\mdfsetup{innertopmargin=5pt,linecolor=blue!20,%
linewidth=2pt,topline=true,%
frametitleaboveskip=\dimexpr-\ht\strutbox\relax
}
\begin{mdframed}[]\relax%
\label{#2}}{\end{mdframed}}

\begin{document}
\title{
\textbf{Test Document}}
\author{
\Large{Latex n00b}}
\setlength{\parskip}{1ex plus 0.5ex minus 0.2ex}
\maketitle

\chapter{First Chapter}
So as I mentioned, the tikz theorem box as defined above cuts the first    letter of the theorem statement. It also splits across pages in an ugly way.
\begin{theorem}
For $p$ be a prime,
$$a^{p-1} \equiv 1 \pmod p$$ 
\end{theorem}
It also shows errors when the theorem statement comprises only an equation.
\begin{theorem}
$$\sum \limits_{i=1}^{n}  i = \frac{n(n+1)}{2}$$ 
\end{theorem}

\end{document}
BharatRam
  • 131
  • 1
    Welcome to TeX.SE! Please help us help you and add a minimal working example (MWE), starting with \documentclass{...} and ending with \end{document} (including only relevant packages) that still illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code. – Troy Mar 15 '17 at 16:55
  • The code does not compile (! Missing $ inserted.<inserted text>$ \end{theorem}) and is not minimal (many packages loaded which are unrelated to your question). Did you actually test the code that you provide? Probably unrelated, $$ should not be used anymore. Use \begin{equation}... instead. See http://tex.stackexchange.com/questions/40492 for details. – Dr. Manuel Kuehner Mar 16 '17 at 01:10
  • @Dr.ManuelKuehner \[...\] is the replacement for $$...$$ in 2e, as far as I know. Of course, a maths environment is another option, but it isn't necessary. – cfr Mar 16 '17 at 02:11
  • Note that the 15pt means 10pt so you might as well omit it. – cfr Mar 16 '17 at 02:16
  • The theroem environment is defined with two parameters, \newenvironment{theorem}[2][] one optional (empty by default) and another mandatory which is used as a label. You need to always type this mandatory parameter which can be empty: \begin{theorem}{}. Otherwise, the first letter of theorem text is used as label. – Ignasi Mar 17 '17 at 09:16

1 Answers1

2

In your code, theorem definition starts with:

\newenvironment{theorem}[2][]{%

which means that the environment will have an initially empty optional parameter and a mandatory one. Therefore, your theorem environments should be:

\begin{theorem}{...}  %<--- No optional parameter and one mandatory
theorem contents
\end{theorem}

As you don't include the mandatory parameter, fist letters of theorem contents are used as it. As soon as you add {} the problem will disappear.

As you say that you're learning LaTeX, may be you cold be interested in tcolorbox package wich provides an special library for boxed theorems. Following code shows how reproduce your theorems format with tcolorbox.

In this case \newtcolorbox declares a theorem environment called myTheorem which will have two mandatory arguments, the first one is theorem title and the second a label sufix. The same command also defines a myTheorem* environment for unnumbered theorems.

\documentclass[15pt]{book}

\usepackage[most]{tcolorbox}

\newtcbtheorem[number within=chapter]{myTheorem}{THEOREM}{%
    enhanced,
    sharp corners,
    colframe=blue!20,
    colback=white,
    colbacktitle=blue!20,
    coltitle=black,
    boxed title style={sharp corners},
    attach boxed title to top left={xshift=5mm,yshift*=-\tcboxedtitleheight/2},
}{th}

\begin{document}

\chapter{First chapter}

\begin{myTheorem*}{}{}
For $p$ be a prime,
\[a^{p-1} \equiv 1 \pmod p\] 
\end{myTheorem*}
\begin{myTheorem}{This is another theorem}{NT}
\[\sum_{i=1}^{n}  i = \frac{n(n+1)}{2}\]
\end{myTheorem}

As you can see in Theorem~\ref{th:NT}
\end{document}

enter image description here

Ignasi
  • 136,588