当命令脆弱时,数学模式以正常模式打印

当命令脆弱时,数学模式以正常模式打印

我已经创建了一个宏,当数学模式下的命令以粗体打印时,如果它在数学之外,它应该以彩色打印

\def\mathtest{\ifmmode ***math mode***\else\textcolor{red}{***text mode***}\fi}

当在 Head 部分使用命令时,输出仅在 Head 部分正确打印,但在运行头中(当宏很脆弱时),它以正常模式打印

\section{Section Head $\mathtest$}

如何获得与头部相同的运行头。不使用\protect

梅威瑟:

\documentclass{book}
\usepackage{color}
\definecolor{RED}{rgb}{1,0,0}    
\begin{document}


\def\mathtest{\ifmmode ***math mode***\else\textcolor{red}{***text mode***}\fi}


\chapter{Sample Chapter}

\section{Section Head $\mathtest$}

As conservation geneticists, we recognize the importance of genetic diversity in maintaining healthy
natural populations, and in facilitating adaptation to new environmental conditions and challenges. However, both population
genetics and the American conservation movement have their roots in the human eugenics movement of a century ago,
which viewed genetic diversity among human populations as grounds for discrimination and prejudice. We acknowledge this
unfortunate part of the history of both population genetics and conservation, and denounce how it has been used to suppress
and disadvantage people.

Many of the early statistical methods that still underlie genetic analysis were developed by devout eugenicists. Francis
Galton, a cousin of Charles Darwin, coined the term eugenics in 1883 (Galton 1883, p. 24). Simply put, the field of eugenics
viewed human traits as the product of genes, some trait variants more valuable than others, and therefore some human races
as better than others (Rohlfs 2020). Galton also developed the concept of linear regression analysis, initially termed “reversion
to the mean” or “reversion to mediocrity,” which remains widely used in analysis of data of many types. Ronald A. Fisher,
who was one of the founders of population genetics, and who developed the statistical method analysis of variance, was also
a staunch eugenicist. Much of The Genetical Theory of Natural Selection (Fisher 1930) was devoted to Fisher’s concern with
the genetic effects of the lower fertility of the English upper class. US President Theodore Roosevelt and his conservation chief
Gifford Pinchot, considered fathers of the conservation movement in their country, were both part of the eugenics movement
(Wohlforth2010).
to eugenics.

Many of the early statistical methods that still underlie genetic analysis were developed by devout eugenicists. Francis
Galton, a cousin of Charles Darwin, coined the term eugenics in 1883 (Galton 1883, p. 24). Simply put, the field of eugenics
viewed human traits as the product of genes, some trait variants more valuable than others, and therefore some human races
as better than others (Rohlfs 2020). Galton also developed the concept of linear regression analysis, initially termed “reversion
to the mean” or “reversion to mediocrity,” which remains widely used in analysis of data of many types. Ronald A. Fisher,
who was one of the founders of population genetics, and who developed the statistical method analysis of variance, was also
a staunch eugenicist. Much of The Genetical Theory of Natural Selection (Fisher 1930) was devoted to Fisher’s concern with
the genetic effects of the lower fertility of the English upper class. US President Theodore Roosevelt and his conservation chief
Gifford Pinchot, considered fathers of the conservation movement in their country, were both part of the eugenics movement
(Wohlforth2010).
to eugenics.


Many of the early statistical methods that still underlie genetic analysis were developed by devout eugenicists. Francis
Galton, a cousin of Charles Darwin, coined the term eugenics in 1883 (Galton 1883, p. 24). Simply put, the field of eugenics
viewed human traits as the product of genes, some trait variants more valuable than others, and therefore some human races
as better than others (Rohlfs 2020). Galton also developed the concept of linear regression analysis, initially termed “reversion
to the mean” or “reversion to mediocrity,” which remains widely used in analysis of data of many types. Ronald A. Fisher,
who was one of the founders of population genetics, and who developed the statistical method analysis of variance, was also
a staunch eugenicist. Much of The Genetical Theory of Natural Selection (Fisher 1930) was devoted to Fisher’s concern with
the genetic effects of the lower fertility of the English upper class. US President Theodore Roosevelt and his conservation chief
Gifford Pinchot, considered fathers of the conservation movement in their country, were both part of the eugenics movement
(Wohlforth2010).
to eugenics.

Many of the early statistical methods that still underlie genetic analysis were developed by devout eugenicists. Francis
Galton, a cousin of Charles Darwin, coined the term eugenics in 1883 (Galton 1883, p. 24). Simply put, the field of eugenics
viewed human traits as the product of genes, some trait variants more valuable than others, and therefore some human races
as better than others (Rohlfs 2020). Galton also developed the concept of linear regression analysis, initially termed “reversion
to the mean” or “reversion to mediocrity,” which remains widely used in analysis of data of many types. Ronald A. Fisher,
who was one of the founders of population genetics, and who developed the statistical method analysis of variance, was also
a staunch eugenicist. Much of The Genetical Theory of Natural Selection (Fisher 1930) was devoted to Fisher’s concern with
the genetic effects of the lower fertility of the English upper class. US President Theodore Roosevelt and his conservation chief
Gifford Pinchot, considered fathers of the conservation movement in their country, were both part of the eugenics movement
(Wohlforth2010).
to eugenics.


Many of the early statistical methods that still underlie genetic analysis were developed by devout eugenicists. Francis
Galton, a cousin of Charles Darwin, coined the term eugenics in 1883 (Galton 1883, p. 24). Simply put, the field of eugenics
viewed human traits as the product of genes, some trait variants more valuable than others, and therefore some human races
as better than others (Rohlfs 2020). Galton also developed the concept of linear regression analysis, initially termed “reversion
to the mean” or “reversion to mediocrity,” which remains widely used in analysis of data of many types. Ronald A. Fisher,
who was one of the founders of population genetics, and who developed the statistical method analysis of variance, was also
a staunch eugenicist. Much of The Genetical Theory of Natural Selection (Fisher 1930) was devoted to Fisher’s concern with
the genetic effects of the lower fertility of the English upper class. US President Theodore Roosevelt and his conservation chief
Gifford Pinchot, considered fathers of the conservation movement in their country, were both part of the eugenics movement
(Wohlforth2010).
to eugenics.

Many of the early statistical methods that still underlie genetic analysis were developed by devout eugenicists. Francis
Galton, a cousin of Charles Darwin, coined the term eugenics in 1883 (Galton 1883, p. 24). Simply put, the field of eugenics
viewed human traits as the product of genes, some trait variants more valuable than others, and therefore some human races
as better than others (Rohlfs 2020). Galton also developed the concept of linear regression analysis, initially termed “reversion
to the mean” or “reversion to mediocrity,” which remains widely used in analysis of data of many types. Ronald A. Fisher,
who was one of the founders of population genetics, and who developed the statistical method analysis of variance, was also
a staunch eugenicist. Much of The Genetical Theory of Natural Selection (Fisher 1930) was devoted to Fisher’s concern with
the genetic effects of the lower fertility of the English upper class. US President Theodore Roosevelt and his conservation chief
Gifford Pinchot, considered fathers of the conservation movement in their country, were both part of the eugenics movement
(Wohlforth2010).
to eugenics.


\clearpage

\end{document} 

答案1

您可以使用一个强大的定义:

\DeclareDocumentCommand{\mathtest}{}{%
  \ifmmode
    % math
  \else
    % not math
  \fi
}

或者,etoolbox提供了一种使命令更强大的方法:

\usepackage{etoolbox}
\robustify\mathtest

相关内容