我想将一个带框的显示方程式相对于页边距居中,该方程式位于枚举或逐项环境中。下面是我所拥有的一个例子。请注意,显示的带框方程式相对于环境(而不是页边距)居中。顺便说一句,我借用了这个问题为了得到方框方程。
\documentclass[11pt]{article}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{color}
\definecolor{myblue}{rgb}{.8, .8, 1}
\usepackage{amsmath}
\usepackage{empheq}
%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%
%Code for boxed equation, taken from a question on Stack Exchange:
\newlength\mytemplen
\newsavebox\mytempbox
\makeatletter
\newcommand\mybluebox{%
\@ifnextchar[%]
{\@mybluebox}%
{\@mybluebox[0pt]}}
\def\@mybluebox[#1]{%
\@ifnextchar[%]
{\@@mybluebox[#1]}%
{\@@mybluebox[#1][0pt]}}
\def\@@mybluebox[#1][#2]#3{
\sbox\mytempbox{#3}%
\mytemplen\ht\mytempbox
\advance\mytemplen #1\relax
\ht\mytempbox\mytemplen
\mytemplen\dp\mytempbox
\advance\mytemplen #2\relax
\dp\mytempbox\mytemplen
\colorbox{myblue}{\hspace{1em}\usebox{\mytempbox}\hspace{1em}}}
\makeatother
%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%
\begin{document}
\title{Title}
\author{Author}
\date{Date}
\maketitle
\noindent Here is a sentence.
\begin{itemize}
\item Here is an item.
\item Here is an item.
\begin{enumerate}
\item Here is Step 1.
\item Here is Step 2 with an equation:
\begin{empheq}[box={\mybluebox[5pt]}]{equation*}
x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}.
\end{empheq}
\item Here is Step 3.
\end{enumerate}
\item Here is an item.
\end{itemize}
\end{document}
答案1
herbert 在如何在列表环境中将文本/数学置于中心位置?(与上面 cmhughes 链接的相同)可以轻松修改以适合您的情况。
\documentclass[11pt]{article}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{showframe} %% just for illustration
\usepackage{color}
\definecolor{myblue}{rgb}{.8, .8, 1}
\usepackage{amsmath}
\usepackage{empheq}
%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%
%Code for boxed equation, taken from a question on Stack Exchange:
\newlength\mytemplen
\newsavebox\mytempbox
\makeatletter
\newcommand\mybluebox{%
\@ifnextchar[%]
{\@mybluebox}%
{\@mybluebox[0pt]}}
\def\@mybluebox[#1]{%
\@ifnextchar[%]
{\@@mybluebox[#1]}%
{\@@mybluebox[#1][0pt]}}
\def\@@mybluebox[#1][#2]#3{
\sbox\mytempbox{#3}%
\mytemplen\ht\mytempbox
\advance\mytemplen #1\relax
\ht\mytempbox\mytemplen
\mytemplen\dp\mytempbox
\advance\mytemplen #2\relax
\dp\mytempbox\mytemplen
\colorbox{myblue}{\hspace{1em}\usebox{\mytempbox}\hspace{1em}}}
%
%% This is the macro for you
\def\MLine#1{\par\hspace*{-\@totalleftmargin}\parbox{\textwidth}{%
\begin{empheq}[box={\mybluebox[5pt]}]{equation*}
#1
\end{empheq}}}
\makeatother
%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%
\begin{document}
\title{Title}
\author{Author}
\date{Date}
\maketitle
\noindent Here is a sentence.
\begin{itemize}
\item Here is an item.
\item Here is an item.
\begin{enumerate}
\item Here is Step 1.
\item Here is Step 2 with an equation:
\MLine{x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}}
\item Here is Step 3.
\end{enumerate}
\item Here is an item.
\end{itemize}
\end{document}
如果您想使用方程式的其他格式(例如无星号的格式),可以将其推广为
\def\MLine#1{\par\hspace*{-\@totalleftmargin}\parbox{\textwidth}{%
#1
}}
并使用如下公式:
\MLine{%
\begin{empheq}[box={\mybluebox[5pt]}]{equation}
x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}
\end{empheq}
}