背景

背景

晚上好,或者早上好,取决于您所在的地方。

背景

我一直在想办法如何让我的 LaTeX 宏在 markdown 中轻松访问(通过 MathJax),而不会降低我对 LaTeX 的标准。具体来说,我希望能够编写准备好的方程式Ctrl+CCtrl+V此外,由于我必须考虑撰写论文,我希望它能够完美地融入我的词汇表和索引系统中。

我使用相当详尽的方法实现了一个最小的工作示例:

  1. 使用 定义符号\newcommand,并制作一个“虚拟”副本以用于词汇表定义。
  2. 定义标签与符号等效相同的词汇表条目。
  3. 重新定义符号定义,使得\Hamiltonian |--> \glssymbol{Hamiltonian}\index{Hamiltonian}

最小示例

排版命令(终端):

filename=asdf.tex
pdflatex ${filename}
makeglossaries  -l ${filename%%.*}
makeindex  ${filename/.*/.idx}
pdflatex ${filename}
pdflatex ${filename}

LaTeX 文件:

\documentclass[b5paper]{memoir}
%
\title{This is a test}
\author{Ola}
% 1 ************** N O T A T I O N -- D E F I N I T I O N ******************* %
\usepackage{amsmath}
%% H
\newcommand{\Hamiltonian}{\ensuremath{\mathcal{H}}}% <-------------- Definition
\let\OldHamiltonian\Hamiltonian% <--------------------- Used in glossary symbol
%% L
\newcommand{\Lagrangian}{\ensuremath{\mathcal{L}}}% <--------------- Definition
\let\OldLagrangian\Lagrangian% <----------------------- Used in glossary symbol
%% P
\newcommand{\vecGeneralMomentum}{\ensuremath{\mathbf{p}}}% <------- Definition
\let\OldVecGeneralMomentum\vecGeneralMomentum% <----- Used in glossary symbol
%% Q
\newcommand{\vecGeneralPosition}{\ensuremath{\mathbf{q}}}% <-------- Definition
\let\OldVecGeneralPosition\vecGeneralPosition% <------- Used in glossary symbol
% 2 ************ G L O S S A R Y -- D E F I N I T I O N ********************* %
\usepackage{hyperref}
\usepackage[%
  nopostdot,%
  nonumberlist,%
  toc,%
  section,%
  acronym,%
]{glossaries}
%% H
\newglossaryentry{Hamiltonian}{% <--------- The same as in \newcommand
  type=main,%
  name={Hamiltonian function},%
  symbol={\OldHamiltonian},% <------------- "dummy" copy of command
  description={Hamiltonian function},%
}%
%% L
\newglossaryentry{Lagrangian}{% <---------- The same as in \newcommand
  type=main,%
  sort={Lagrangian},%
  name={Lagrangian function},%
  symbol={\OldLagrangian},% <-------------- "dummy" copy of command
  description={Lagrangian function},%
}%
%% P
\newglossaryentry{vecGeneralPosition}{% <-- The same as in \newcommand
  type=main,%
  sort={Pos},%
  name={position vector},%
  symbol={\OldVecGeneralPosition},% <------ "dummy" copy of command
  description={Generalised position vector},%
}%
%% Q
\newglossaryentry{vecGeneralMomentum}{% <--- The same as in \newcommand
  type=main,%
  name={momentum vector},%
  symbol={\OldVecGeneralMomentum},% <------- "dummy" copy of command
  description={Generalised momentum vector},%
}%
% ... Prettify the glossary table...
\newglossarystyle{mylong2col}{%
  % put glossary in a longtable
  \renewenvironment{theglossary}%
    {%
     \setlength\LTleft{0pt}
     \setlength\LTright{0pt}
     \begin{longtable}[l]{@{\extracolsep{\fill}}l|p{\glsdescwidth}@{}}%
    }%
    {\end{longtable}}%
  % Set the table header
   \renewcommand*{\glossaryheader}{%
    \bfseries Symbol &%
    \bfseries Description
    \\ \hline \endhead}%
   \renewcommand*{\glsgroupheading}[1]{}%
   \renewcommand*{\glossentry}[1]{%
      \glsentryitem{##1}%                              Entry number if required
      \glstarget{##1}{\glossentrysymbol{##1}}%                           Symbol
        & \glossentrydesc{##1}% Description
        \tabularnewline % end of row
   }%
  \renewcommand*{\glsgroupskip}{}%
}
%
\makeglossaries
\makeindex
%
% 3 ************ R E D E F I N E  --  D E F I N I T I O N S ****************** %
%% H
\renewcommand{\Hamiltonian}{\glssymbol{Hamiltonian}\index{\glsentryfirst{Hamiltonian}}}
%% L
\renewcommand{\Lagrangian}{\glssymbol{Lagrangian}\index{\glsentryfirst{Lagrangian}}}
%% P
\renewcommand{\vecGeneralMomentum}{\glssymbol{vecGeneralMomentum}\index{\glsentryfirst{vecGeneralMomentum}}}
%% Q
\renewcommand{\vecGeneralPosition}{\glssymbol{vecGeneralPosition}\index{\glsentryfirst{vecGeneralPosition}}}
% ===================== B E G I N   D O C U M E N T  ======================== %
\begin{document}
%
% ----------------------- F R O N T - M A T T E R --------------------------- %
\frontmatter
%
\maketitle
%
\printglossary[style=mylong2col]
%
% ------------------------ M A I N - M A T T E R ---------------------------- %
%
\mainmatter
%
\chapter{Classical Mechanics}
$$
  \Hamiltonian := \mathrm{d}\left(\vecGeneralPosition \vecGeneralMomentum \right) - \Lagrangian
$$
% -------------------------- A P P E N D I X -------------------------------- %
\appendix
%
% ------------------------- B A C K - M A T T E R --------------------------- %
\backmatter
\printindex
%
\end{document}

问题

我想使用词汇表包的功能将上面的第三步替换为自动循环。

我打算使用forallglsentries,即类似这样的操作:

\forallglsentries[main]{\glslabel}{%
  \renewcommand{\glslabel}{\glssymbol{\glslabel}\index{\glsentryfirst{\glslabel}}}
}%

但是,在这里我无法\csname \oldSymbol\endcsname工作,这是必要的,因为\oldSymbol扩展为“Hamiltonian”,而renewcommand需要“\Hamiltonian”。

对于如何处理这种情况的任何想法都将不胜感激。


附加问题

我怀疑“步骤 2”也是多余的,但我没有看到在不引入循环引用的情况下解决该问题的明显方法,因为我要求定义(\newcommand)中给出的名称也应该是文本中使用的名称。

有没有办法可以将新命令的内容“复制/扩展”到symbol词汇表条目的键中?

答案1

和 构建命令序列的常见问题\renewcommand是缺少 的扩展\csname ...\endcsname,因此使用\expandafter\renewcommand\csname\oldSymbol\endcsname{...}

然而,这还不够,除非该\oldSymbol值在内部使用之前被“预先”扩展,否则,它会导致错误或应用\glssymbol错误的值(即始终是循环中的最后一个)。\forallglsentries

\documentclass[b5paper]{memoir}
%
\title{This is a test}
\author{Ola}
% 1 ************** N O T A T I O N -- D E F I N I T I O N ******************* %
\usepackage{amsmath}
%% H
\newcommand{\Hamiltonian}{\ensuremath{\mathcal{H}}}% <-------------- Definition
\let\OldHamiltonian\Hamiltonian% <--------------------- Used in glossary symbol
%% L
\newcommand{\Lagrangian}{\ensuremath{\mathcal{L}}}% <--------------- Definition
\let\OldLagrangian\Lagrangian% <----------------------- Used in glossary symbol
%% P
\newcommand{\vecGeneralMomentum}{\ensuremath{\mathbf{p}}}% <------- Definition
\let\OldVecGeneralMomentum\vecGeneralMomentum% <----- Used in glossary symbol
%% Q
\newcommand{\vecGeneralPosition}{\ensuremath{\mathbf{q}}}% <-------- Definition
\let\OldVecGeneralPosition\vecGeneralPosition% <------- Used in glossary symbol
% 2 ************ G L O S S A R Y -- D E F I N I T I O N ********************* %
\usepackage{hyperref}
\usepackage[%
  nopostdot,%
  nonumberlist,%
  toc,%
  section,%
  acronym,%
]{glossaries}
%% H
\newglossaryentry{Hamiltonian}{% <--------- The same as in \newcommand
  type=main,%
  name={Hamiltonian function},%
  symbol={\OldHamiltonian},% <------------- "dummy" copy of command
  description={Hamiltonian function},%
}%
%% L
\newglossaryentry{Lagrangian}{% <---------- The same as in \newcommand
  type=main,%
  sort={Lagrangian},%
  name={Lagrangian function},%
  symbol={\OldLagrangian},% <-------------- "dummy" copy of command
  description={Lagrangian function},%
}%
%% P
\newglossaryentry{vecGeneralPosition}{% <-- The same as in \newcommand
  type=main,%
  sort={Pos},%
  name={position vector},%
  symbol={\OldVecGeneralPosition},% <------ "dummy" copy of command
  description={Generalised position vector},%
}%
%% Q
\newglossaryentry{vecGeneralMomentum}{% <--- The same as in \newcommand
  type=main,%
  name={momentum vector},%
  symbol={\OldVecGeneralMomentum},% <------- "dummy" copy of command
  description={Generalised momentum vector},%
}%
% ... Prettify the glossary table...
\newglossarystyle{mylong2col}{%
  % put glossary in a longtable
  \renewenvironment{theglossary}%
    {%
     \setlength\LTleft{0pt}
     \setlength\LTright{0pt}
     \begin{longtable}[l]{@{\extracolsep{\fill}}l|p{\glsdescwidth}@{}}%
    }%
    {\end{longtable}}%
  % Set the table header
   \renewcommand*{\glossaryheader}{%
    \bfseries Symbol &%
    \bfseries Description
    \\ \hline \endhead}%
   \renewcommand*{\glsgroupheading}[1]{}%
   \renewcommand*{\glossentry}[1]{%
      \glsentryitem{##1}%                              Entry number if required
      \glstarget{##1}{\glossentrysymbol{##1}}%                           Symbol
        & \glossentrydesc{##1}% Description
        \tabularnewline % end of row
   }%
  \renewcommand*{\glsgroupskip}{}%
}
%
\makeglossaries
\makeindex
%
% 3 ************ R E D E F I N E  --  D E F I N I T I O N S ****************** %
%% H
%\renewcommand{\Hamiltonian}{\glssymbol{Hamiltonian}\index{\glsentryfirst{Hamiltonian}}}
%% L
%\renewcommand{\Lagrangian}{\glssymbol{Lagrangian}\index{\glsentryfirst{Lagrangian}}}
%% P
%\renewcommand{\vecGeneralMomentum}{\glssymbol{vecGeneralMomentum}\index{\glsentryfirst{vecGeneralMomentum}}}
%% Q
%\renewcommand{\vecGeneralPosition}{\glssymbol{vecGeneralPosition}\index{\glsentryfirst{vecGeneralPosition}}}
% ===================== B E G I N   D O C U M E N T  ======================== %
\begin{document}
%
% ----------------------- F R O N T - M A T T E R --------------------------- %
\frontmatter
%
\maketitle
%
\printglossary[style=mylong2col]
%
% ------------------------ M A I N - M A T T E R ---------------------------- %
%
\mainmatter

\def\oldSymbol{}
\newcommand{\redefinethestuff}[1]{%
   \expandafter\renewcommand\csname#1\endcsname{\glssymbol{#1}\index{\glsentryfirst{#1}}}
}
\forallglsentries[main]{\oldSymbol}{%
  \expandafter\redefinethestuff\expandafter{\oldSymbol}%
}%

\chapter{Classical Mechanics}
\[
  \Hamiltonian := \mathrm{d}\left(\vecGeneralPosition \vecGeneralMomentum \right) - \Lagrangian
\]
% -------------------------- A P P E N D I X -------------------------------- %
\appendix
%
% ------------------------- B A C K - M A T T E R --------------------------- %
\backmatter
\printindex
%
\end{document}

请不要使用$$...$$but \[ ... \]

相关内容