如何定义带有空格的变量

如何定义带有空格的变量

我正在编写一个 LaTex 文档,其中必须在文档的某些点引用一些变量。为了确保每个变量都以相同的方式编写,我为每个变量定义了一个 LaTex 变量。

\documentclass[border=10pt]{standalone}
\usepackage{algorithm,algpseudocode}

\newcommand{\varu}{\emph{u}}
\newcommand{\varv}{\emph{v}}
\newcommand{\varz}{\emph{z}}
\newcommand{\varTr}{\emph{Training}}
\newcommand{\varRzT}{$R_{\varz_{\varTr}$ } }
\newcommand{\varReaz}{$R_{EAz}$ }
\newcommand{\varRecScore}{$s(\varv, \vart | \varu)$ }
\newcommand{\varCS}{Cand Set}
\newcommand{\varw}{\emph{w}(\varu,\varv)}
\newcommand{\varBC}{$\mathrm{BC}_{suff}$}

\begin{document}
My text is quite long. In the text there can 
be referred some variables like \varu
which I would like is written as emph and after
the variable there is a white space.

Some of these variables are also inserted into 
an algorithm structure defined with algorithm.

\end{document}

我在文档的某些部分使用了这些变量:在文本中、在数学模式中以及在算法结构中。我的问题是,如果我在文本中使用这些变量,它们后面没有空格。(即变量后面\varu没有空格)。

我该如何解决?我无法\varu用空格进行定义\newcommand{\varu}{\emph{u }},因为如果变量后面跟着“。”,空格就不正确。

答案1

您的方法在很多方面都是错误的。

  1. 您应该使用数学斜体,而不是\emph,多字母变量除外;另一方面,“培训”是文本,因此应该是直立的。

  2. 数学公式应该处于数学模式,即使它只是一个变量。

\newcommand{\varu}{u}
\newcommand{\varv}{v}
\newcommand{\varz}{z}
\newcommand{\varTr}{\mathrm{Training}}
\newcommand{\varRzT}{R_{\varz_{\varTr}}}
\newcommand{\varReaz}{R_{EAz}}
\newcommand{\varRecScore}{s(\varv,\vart\mid\varu)}
\newcommand{\varCS}{\mathrm{Cand\ Set}}
\newcommand{\varw}{w(\varu,\varv)}
\newcommand{\varBC}{\mathrm{BC}_{\mathrm{suff}}}

那么你的文字可能更简单,

My text is quite long. In the text there can 
be referred some variables like $\varu$
and after the variable there is a white space.
\begin{equation}
(\varu,\varv) = \varu * \varv - \varRzT
\end{equation}

答案2

\usepackage{xspace}
\newcommand\varu{$u$\xspace}

答案3

LaTeX 会忽略命令后的空格。为了确保在需要时获得空格,请使用\varu\. 即在空格前添加反斜杠。

相关内容