检测 LaTeX 类名

发布于 2024-08-26 16:23:13 字数 457 浏览 2 评论 0原文

我正在开发一个 LaTeX 包,它可能需要根据所使用的类以不同的方式执行一些操作。我想知道是否有一种方法可以自动检测或测试文档类。

人们当然可以查找类文件并测试该类定义的特定宏是否存在,但是有没有更聪明的方法呢?我查看了 \ProvidesClass 宏的定义,看不出它是否将类名保存在除 \@currname 之外的任何位置。我相信 \@currname 只是当前正在读取的包或类的名称。

基本上我想

\author{\longauthorname}

article 类中执行,但

\author[\shortauthorname]{\longauthorname}

beamer 类中执行。

I'm working on a LaTeX package which might need to do some things differently depending on the class that's being used. I'm wondering if there's a way to auto-detect or test the document class.

One could certainly look up the class files and test for the existence of a specific macro defined by that class, but is there a smarter way? I looked at the definition of the \ProvidesClass macro and can't see if it saves the class name anywhere except \@currname. I believe \@currname is just the name of the current package or class being read.

Basically I want to execute

\author{\longauthorname}

in the article class but

\author[\shortauthorname]{\longauthorname}

in the beamer class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丢了幸福的猪 2024-09-02 16:23:13

完善我的问题后,我将展示我是如何回答的。沿着 dmckee 所说的思路。只需测试功能即可。

\ifcsname beamer@author\endcsname
  \author[\shortauthorname]{\longauthorname}
\else
  \author{\longauthorname}
\fi

\ifcsame 可用于所有 e-TeX 版本,并已记录(以及检查命令是否已定义的其他方法)此处

您无法检查 \author 宏的实际签名(即,它是否需要可选参数?),但您可以检查一些定义为实现可选参数的辅助宏。 \beamer@authorbeamer 类中的其中之一。

After refining my question I'll show how I answered it. Along the lines of what dmckee was saying. Just test for the functionality.

\ifcsname beamer@author\endcsname
  \author[\shortauthorname]{\longauthorname}
\else
  \author{\longauthorname}
\fi

\ifcsame is available on all e-TeX builds and is documented (along with other ways to check if a command is defined) here.

You can't check for the actual signature of the \author macro (i.e., does it take an optional argument?) but you can check for some of the auxiliary macros defined to implement optional arguments. \beamer@author is one of those in the beamer class.

独闯女儿国 2024-09-02 16:23:13

恕我直言,您不应该检查您的类(或版本)的名称。
您应该检查功能。

例如,article 类具有 \@titlepagefalse
book 类具有 \@titlepagetrue
书写

\if@titlepage yes \else no \fi

并识别扉页的存在。

IMHO, you should not check the name of your class (or version).
You should check the functionality.

For example, class article has \@titlepagefalse and
class book has \@titlepagetrue.
Write

\if@titlepage yes \else no \fi

and recognize the presence of title page.

扛起拖把扫天下 2024-09-02 16:23:13

对于加载的文档类有一个简单的测试: \@ifclassloaded{beamer}{}{}

简短示例:

%\documentclass{article}
\documentclass{beamer}

\newcommand{\longauthorname}{foo}
\newcommand{\shortauthorname}{bar}


\makeatletter
\@ifclassloaded{beamer}{%
    \author[\shortauthorname]{\longauthorname}
}{
 \author{\longauthorname}
}
\makeatother


\begin{document}

test

\end{document}

There is an easy test for the loaded document class: \@ifclassloaded{beamer}{<true>}{<false>}

Short example:

%\documentclass{article}
\documentclass{beamer}

\newcommand{\longauthorname}{foo}
\newcommand{\shortauthorname}{bar}


\makeatletter
\@ifclassloaded{beamer}{%
    \author[\shortauthorname]{\longauthorname}
}{
 \author{\longauthorname}
}
\makeatother


\begin{document}

test

\end{document}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文