悠闲数学娱乐论坛(第2版)'s Archiver

abababa 发表于 2020-11-23 17:43

请教怎么自动识别documentclass

如题。因为之前打了几个文档,还有一些书,觉得那些包这篇引入了,那篇又没引入,不太方便,现在就把这些包都做成一个base.tex,然后想这样引入:[code]\documentclass[10pt]{article}
\input{base}
\begin{document}
\end{document}[/code]然后在base.tex里有一个:
\renewcommand{\chaptername}{}

这个只能在book类型的文档中使用,能不能类似下面这样:
\if documentclass=book
\renewcommand{\chaptername}{}
\fi

让这个base.tex在book和article里都能直接引用,其它的还有一些对chapter的设置也是,都想放到那个if里。这个要怎么做?

kuing 发表于 2020-11-23 22:21

那你可以通过判断 \chapter 是否存在来确定是不是 article

举个简单栗子:[code]\documentclass{article}
\newif\ifart
\ifx\chapter\undefined\arttrue\fi
\begin{document}
the document class is
\ifart
article
\else
not article
\fi
\end{document}[/code]将文档类换成 book 就会有不同结果。

abababa 发表于 2020-11-24 10:01

[b]回复 [url=http://kuing.orzweb.net/redirect.php?goto=findpost&pid=37589&ptid=7512]2#[/url] [i]kuing[/i] [/b]

谢谢,原来这么多if类型,经过2楼提示,在网上找到一个ifdefined
不过还有些地方不太明白,猜测这个ifdefined应该是判断chapter是否定义过,如果定义过就把中间的执行了,但我下面的代码并不能运行。如果把\ifdefined\chapter改成\ifdefined\chaptername,这样就能成功运行了,也确实是我需要的效果:在article里就不管chapter了,在book里才使用定义的样式。那这个\chapter到底在哪定义过呢?为什么article里也找到它了?[code]
\documentclass[10pt]{article}
\usepackage[small,center,pagestyles]{titlesec}

\ifdefined\chapter
\titleformat{\chapter}[hang]{\Large\bfseries}{\chaptername}{1em}{}
\renewcommand{\chaptername}{}
\fi
\begin{document}
\tableofcontents

\chapter{测试章}
\section{测试节}
测试文本
\end{document}
[/code]

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.