tikz 定理格式的一个问题

tikz 定理格式的一个问题

我是 Latex 的新手,正在通过使用模板和尝试各种参数和参数来学习更多工具。

因此,我尝试使用以下 tikz 构造来对定理进行装箱

\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}}

这样做会遮挡定理陈述中的前几个字母。此外,它经常会以丑陋的方式分裂到不同的页面上。我尝试调整参数和边距等,但无济于事。

有没有办法来解决这个问题?

这是一个示例文档,您可以复制并编译它来亲自查看错误。

\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}

答案1

在您的代码中,定理定义以此开头:

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

这意味着环境将有一个最初为空的可选参数和一个强制参数。因此,你的定理环境应该是:

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

由于您没有包含强制参数,因此将使用定理内容的第一个字母作为强制参数。一旦添加,{}问题就会消失。

正如你所说,你正在学习 LaTeX,也许你会对tcolorbox包提供了一个用于盒装定理的特殊库。以下代码显示了如何使用重现定理格式tcolorbox

在本例中,\newtcolorbox声明了一个名为的定理环境myTheorem,它将具有两个强制参数,第一个是定理标题,第二个是标签后缀。同一命令还定义了一个myTheorem*未编号定理的环境。

\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}

在此处输入图片描述

相关内容