我正在尝试创建一个带有阴影背景的定理样式。由于我已经有一个可用的序言,我希望尽可能少地进行更改。之前我定义了一个自己的定理样式“自定义”,用于定理等。但是,由于这不会为我的定理添加阴影,因此它不再是一个选项。
现在我尝试定义一个新的定理样式“shaded”,它应该可以做到这一点。不幸的是,它没有按照我想要的方式工作。它没有使用新定义的定理样式,而是使用某种默认的定理样式。我希望有人能帮我解决这个问题(尽可能少做改动,并提供详细的解释,因为我对 latex 还只是个初学者)。
我知道这并不是一个真正的“最低限度工作”示例,我希望它仍然没问题。
\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}
答案1
存在几个问题,其中主要包括前两个问题:
您需要
thmtools
在 之后加载amsthm
。由于您使用了
\declaretheoremstyle
样式shaded
,因此您需要使用\declaretheorem
而不是 来定义您的结构\newtheorem
,并将shaded
样式作为选项传递给style
键而不是使用\theoremstyle
。在最后的位置加载
hyperref
(除了在这里其他地方可以找到的一些例外情况)。由于您使用的是 KOMA 类,因此
parskip
您应该使用parskip
类选项而不是加载包;在我的示例中我使用了parskip=half+
但您可以使用所需的值(请参阅 KOMA 用户指南以查看可用选项)。
修正后的代码(根据需要调整设置):
\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}
结果:
结果: