原始 MWE

原始 MWE

正如最初在这里发布的,我想用\exp在数学模式下生成一些指数表达式,但是由于gb4e(我也想用它进行行间注释)覆盖了有问题的命令,所以我遇到以下错误消息;

! LaTeX Error: Command \item invalid in math mode.

我应该怎么做才能\exp再次使用数学模式,或者更具体地说,我应该如何避免\exp数学模式和的冲突gb4e

我正在使用 RMarkdown XeLaTeX,因此我将 MWE 作为.Rmd文件样式。

原始 MWE

---
documentclass: bxjsarticle  
classoption: pandoc,everyparhook=compat
output:
  bookdown::pdf_document2:
    latex_engine: xelatex
    keep_tex: true
header-includes: 
  - \makeatletter 
  - \def\new@fontshape{}
  - \makeatother
  - \usepackage{gb4e}\noautomath
title: "Example"
---

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.

$$
\exp(3.576)
$$

MWE(.tex版本)

\documentclass[pandoc,everyparhook=compat]{bxjsarticle}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
\else % if luatex or xelatex
  \ifxetex
    \usepackage{mathspec}
  \else
    \usepackage{fontspec}
  \fi
  \defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\fi
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\usepackage{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\usepackage{hyperref}
\hypersetup{unicode=true,
            pdftitle={Example},
            pdfborder={0 0 0},
            breaklinks=true}
\urlstyle{same}  % don't use monospace font for urls
\usepackage{longtable,booktabs}
\usepackage{graphicx,grffile}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
}
\setlength{\emergencystretch}{3em}  % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{5}
% Redefines (sub)paragraphs to behave more like sections
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi

%%% Use protect on footnotes to avoid problems with footnotes in titles
\let\rmarkdownfootnote\footnote%
\def\footnote{\protect\rmarkdownfootnote}

%%% Change title format to be more compact
\usepackage{titling}

% Create subtitle command for use in maketitle
\providecommand{\subtitle}[1]{
  \posttitle{
    \begin{center}\large#1\end{center}
    }
}

\setlength{\droptitle}{-2em}

  \title{Example}
    \pretitle{\vspace{\droptitle}\centering\huge}
  \posttitle{\par}
    \author{}
    \preauthor{}\postauthor{}
    \date{}
    \predate{}\postdate{}

\makeatletter
\def\new@fontshape{}
\makeatother
\usepackage{gb4e}

\noautomath

\begin{document}
\maketitle

{
\setcounter{tocdepth}{2}
\tableofcontents
}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.

\[
\exp(3.576)
\]


\end{document}

尝试并尝试

回答为了避免名称冲突,我曾尝试重新定义\exp,但无济于事。

\let\oldexp\exp
\protected\def\exp{\ifmmode\exp\else\expandafter\oldexp\fi}

答案1

我从来没有用过gb4e,但看看它的定义\exp

% gb4e.sty line 319
\def\exp#1{\exi{{(\ref{#1}$'$)}}}

我认为下面的方法应该可行(希望我为其做出了有意义的 MWE gb4e

\documentclass{article}

\let\mathexp=\exp % save the current (math) definition of \exp
\usepackage{gb4e}
\let\gbexp=\exp % save the current (gb4e) definition of \exp

\DeclareRobustCommand{\exp}{\ifmmode\mathexp\else\expandafter\gbexp\fi}

\begin{document}

$\exp(x)$

\begin{exe}
\ex\label{here} Bar foo baz.
\exp{here}{Foo bar baz.}
\end{exe}

\end{document}

在此处输入图片描述

相关内容