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}

\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! 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\[...\]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:1115ptmeans10ptso you might as well omit it. – cfr Mar 16 '17 at 02:16theroemenvironment 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