TeX&LaTeX宏包冲突集锦

时间:2022-06-23 18:48:23
宏包冲突问题,是非常棘手的问题,也是令我非常难过的问题。代码上没问题,但是会报错。这里总结下来,一来自己备忘,二来以飨读者!欢迎大家提供更多这方面的讯息!

Hyperref 至今为止,Hyperref宏包是产生冲突最多的包,自己解决这些问题时,务必读一下该宏包所提供的自述文件(hyperref README)第六章关于兼容性的部分,其中包含了许多冲突和解决方案。
Hyperref 和preamble pages报错讯息
pdfTeX warning (ext4): destination with the same identifier (name{page.2}) has been already used, duplicate ignored详情描述
如果你用一篇文章, 使用了\frontmatter, \mainmatter ,\backmatter命令,在扉页的页码样式是罗马数字( i, ii, iii, iv, v, vi,......) 正文中页码使用的是阿拉伯数字样式( 1, 2, 3, 4, ...)。但是,页码的标签在这两种情况下都是一样的。(page.1, page.2, page.3, etc.),这就会产生错误了,为了解决这个问题,我们应该告诉Hyperref宏包页码标签样式应该是(page.i, page.ii, page.iii, etc.)
解决方案
大家也可以查看LaTeX FAQ  Hyperref and repeated page numbers.解决方案如下:
  1. \usepackage[plainpages=false]{hyperref}[/size][size=10.5pt]
复制代码
Hyperref 和 Fancy headers
报错讯息
pdfTeX warning (ext4): destination with the same identifier (name{figure.1.1}) has been already used, duplicate ignored 详情描述 如果你得到提示是如上错误,即使你的文章中只用一个图 1.1,这里很可能是Hyperref 和 Fancy headers的冲突。

解决方案 hyperref应该在fancyhdr后面加载。 如下:
  1. \usepackage{fancyhdr}
  2. \usepackage[pdftex,colorlinks=false,plainpages=false]{hyperref}

复制代码
Hyperref and Algorithm 报错讯息 LaTeX gives an error in lines with a \ref{} command that points to an algorithm:
undefined control sequence
详情描述
如果你是在latex运行的第二次出现如上错误,有可能是hyperref和algorithm宏包的冲突。
这里简述错误原因:
如果algorithm宏包 加载在hyperref后面,任何\label{alg:myalgorithm}语句都会在.aux文件里产生一个错误语句(包含一个虚假的\theAlgorithm语句)。所以在第二次运行,也就是源文件需要导入aux辅助文件的时候产生如上错误。可参看hyperref的自述文件。
解决方案
hyperref宏包应该在algorithm宏包前,在float宏包后加载。如下即可解决:
  1. \usepackage{float}
  2. \usepackage{hyperref}
  3. \usepackage{algorithm}

复制代码

Hyperref and Algorithmic
报错讯息
pdfTeX warning (ext4): destination with the same identifier (name{ ALC@rem.1}) has been already used, duplicate ignored

如果你的文档中包含多个算法描述,每个算法的行号标签都一样,hyperref会产生如上错误。

请参看<a href="http://developer.berlios.de/feature/?func=detailfeature&feature_id=2973&group_id=3442">fix hyperref warnings with line numbers</a>

解决方案
在algorithm宏包里有一个bug,上述网站有针对2006/06/02版本的不同做法。下面用一个不同上述方法。我们把如下代码拷贝到一个文件里,保存为algorithmic.diff并使用diff< algorithmic.diff 来应用它 (当然事先你要备份一下原来的algorithmic.sty)

  1. --- algorithmic.sty     2006-06-02 00:00:00.000000000 +0000
  2. +++ algorithmic.sty     2006-12-15 00:00:00.000000000 +0000
  3. @@ -16,7 +16,7 @@
  4. % Boston, MA  02111-1307, USA.
  5. %
  6. \NeedsTeXFormat{LaTeX2e}
  7. -\ProvidesPackage{algorithmic}[2006/06/02]
  8. +\ProvidesPackage{algorithmic}[2006/12/15]
  9. \typeout{Document Style `algorithmic' - environment}
  10. %
  11. \RequirePackage{ifthen}
  12. @@ -26,6 +26,7 @@
  13. \setboolean{ALC@noend}{false}
  14. \newcounter{ALC@line}
  15. \newcounter{ALC@rem}
  16. +\newcounter{ALC@unique}
  17. \newcounter{ALC@depth}
  18. \newlength{\ALC@tlm}
  19. %
  20. @@ -128,7 +129,19 @@
  21.      }
  22.    }
  23.    {\end{list}}
  24. -  \newcommand{\ALC@it}{\refstepcounter{ALC@rem}\refstepcounter{ALC@line}\ifthenelse{\equal{\arabic{ALC@rem}}{#1}}{\setcounter{ALC@rem}{0}}{}\item\ALC@setref}
  25. +  \newcommand{\ALC@it}{\@ifundefined{href}{% if not hyperref then do a simple refstepcounter
  26. +    \refstepcounter{ALC@rem}%
  27. +}{% else if hyperref, do the anchor so 2 lines in two differents algorithms cannot have the same href
  28. +   
  29. +\stepcounter{ALC@unique}\stepcounter{ALC@rem}\Hy@raisedlink{\hyper@anchorstart{ALC@rem\theALC@unique.\theALC@rem}\hyper@anchorend}%
  30. +}%
  31. +\@ifundefined{href}{% if not hyperref then do a simple refstepcounter
  32. +    \refstepcounter{ALC@line}%
  33. +}{% else if hyperref, do the anchor so 2 lines in two differents algorithms cannot have the same href
  34. +   
  35. +\stepcounter{ALC@unique}\stepcounter{ALC@line}\Hy@raisedlink{\hyper@anchorstart{ALC@line\theALC@unique.\theALC@line}\hyper@anchorend}%
  36. +}%
  37. +\ifthenelse{\equal{\arabic{ALC@rem}}{#1}}{\setcounter{ALC@rem}{0}}{}\item\ALC@setref}
  38.    \newcommand{\ALC@com}[1]{\ifthenelse{\equal{##1}{default}}%
  39. {}{\ \algorithmiccomment{##1}}}
  40.    \newcommand{\REQUIRE}{\item[\algorithmicrequire]}

复制代码

Algorithms and Pseudocode 报错讯息
A bunch of sybmols (e.g. \IF \TRUE etc) are defined by both packages
解决方案
文章没有完成。
PGF 和 double subscripts 报错讯息 Latex Error: ./mydocument.tex:8 Double subscript.
非常奇怪的是这可能是由于pdf宏包版本较旧引起的,我们之所以奇怪因为pdf只是一个绘图的宏包,但是他却影响了我们的数学公式。下面的代码是一个小例子,运行到第二个sum语句时会产生错误。
  1. \documentclass{article}
  2. \usepackage{pgf}
  3. \begin{document}
  4. The grand total is:
  5. \begin{equation}
  6. s = \sum_{y} \: \sum_{x} x*y
  7. \end{equation}
  8. \end{document}
复制代码
解决方案 PGF1.10版本会产生这一问题,只需要升级到PGF2.0版即可,或者我们稍微修正一下公式,把公式中的\: 改为\;即可解决这一问题。

XeLaTeX 和 pdftex driver 报错讯息
hpdftex.def:442 Undefined control sequence. with line causing the error: \pdfoutput=1
解决方案
如果我们使用xelatex来作为我们的排版程序的时候,往往我们还会给一些宏包制定pdftex的参数。如: %!TEX TS-program = xelatex

\usepackage[pdftex]{graphicx}
\usepackage[pdftex]{hyperref}
这会产生冲突的,XeTeX不能和pdfTeX驱动能很好地兼容。所以我们可以用如下代码: %!TEX TS-program = xelatex

\usepackage[xetex]{graphicx}
\usepackage[xetex]{hyperref}

值得注意的是在XeTeX0.996的补丁1以后版本可以完美解决这一问题,直到XeTeX0.996,苹果的Mac OS X的graphics的默认参数是xdv2pdf。但是自从0.997版本,各个系统里的都已改为xdvipdfmx。我的理解是,选项[XeTeX]选用xdv2pdf作为驱动,而[dvipdfmx]则用xdvipdfmx作为驱动。如果你使用的是更为新的版本,请告诉我这一方法是否仍然适用。
XltXtra and Graphicx
报错讯息 LaTeX Error: Option * for package graphicx. 详情描述
引入xltxtra宏包 (并加载fontspec, xunicode 并定义了 \xetex 短命令) 同时又加载了 graphicx宏包. 但没有有指定任何参数, 一旦你随后指定了一些参数就会产生冲突。
\usepackage{xltxtra} \usepackage[xetex]{graphicx} 解决方案 方案一就是不要加载graphicx,或者是加载的时候不指定任何参数如下:
  1. [size=10.5pt]\usepackage{xltxtra}[/size][size=10.5pt][/size][size=10.5pt]\usepackage{graphicx}[/size][size=10.5pt][/size]
复制代码
方案二比较好一些,就是把graphicx宏包加载在xltxtra宏前,就可以指定参数了,如:
  1. [size=10.5pt]\usepackage[xetex]{graphicx}[/size][size=10.5pt][/size][size=10.5pt]\usepackage{xltxtra}[/size][size=10.5pt][/size]
复制代码

Amssymb with Xunicode 参见: http://bbs.chinatex.org/forum.php?mod=viewthread&tid=4533&extra=page%3D2
Caption 和 Subfig 报错讯息 Option * for package caption. 解决方案 caption实际已经由其他latex宏包加载了。你无需给他指定特定的参数。同样,如果你已引入了subfig宏包。caption宏包要加载在subfig前面。代码如下:
  1. \usepackage[format=plain,labelfont=bf,up,textfont=it,up]{caption}
  2. \usepackage{subfig}

复制代码
TOCLoft and Subfig
报错讯息
Latex Error: tocloft.sty:609 Command \c@lotdepth already defined.
解决方案 这里的错误时TOCLoft和Subfig宏包的冲突,我们的解决方案是给tocloft宏包指定一个参数subfigure即可。
  1. [align=left][size=10.5pt]\usepackage{subfig}[/size][/align][align=left][size=10.5pt]\usepackage[titles,subfigure]{tocloft}[/size][/align]
复制代码

Txfonts and Amsmath 报错讯息! LaTeX Error: Command \iint already defined.
               Or name \end... illegal, see p.192 of the manual.
解决方案
http://www.tex.ac.uk/cgi-bin/texfaq2html?label=alreadydef

Fontspec and Inputenc 如下代码是完全多余的。
\usepackage[utf8]{inputenc} \usepackage{fontspec}
fontspec宏包需要XeTeX或者XeLaTeX模式下。XeTeX需要UTF8的字符输入。因此指定inputenc参数完全是多余的。尽管还没发现出现报错信息,据说可能会致使意外的错误。如果你使用fontspec,就不应再使用inputenc了。如果你坚持使用,需要更明晰的unicode的支持,可以尝试使用xunicode宏包来替代(尽管我发现即使不用,XeTeX还是能理解所输入的utf8字符的。) \usepackage{fontspec} \usepackage{xunicode} 几乎等价于: \usepackage{xltxtra}   % loads graphicx, fontspec, xunicode and others
XeLaTeX and Babel Babel还不能和XeTeX互相兼容。 如:\usepackage[german]{babel}

\font\wsc="Arno Pro"
会产生如下错误:

! Font \wsc= not loadable: Metric (TFM) file or installed font not found.

主要是由于babel修改了“的含义,使得("o替代了\"o).
解决方案是使用fontspec的\setmainfont或者用\fontspec替代\font,近来避免使用引号。
  1. \usepackage[german]{babel}
  2. \usepackage{fontspec}

  3. \setromanfont{Arno Pro}

复制代码
或使用polyglossia替代babel。且polyglossia和XeTeX搭配地很好。
  1. \usepackage[german]{polyglossia}
  2. \usepackage{fontspec}

  3. \setromanfont{Arno Pro}

复制代码

Hyperref and fncychap
报错讯息
In the PDF Table of Contents, the link and page number for Appendix 1 points to Chapter 1, even though the name is correct. 解决方案 原因主要是宏包的不兼容。 只需hyperref放在fncychap后面加载。
  1. \usepackage[PetersLenny]{fncychap}
  2. \usepackage[plainpages=false,pdfpagelabels]{hyperref}

复制代码

Fancy Chapters (fncychap) and Polyglossia 我们经常使用fncychap来修改章标题样式,如下代码:
  1. \usepackage{fncychap}
  2. \ChTitleVar{\Huge\bfseries\sffamily}  % Use Sans Serif for chapter titles

复制代码
系统报错如下: All chapter headers are in sans serif font, except for the table of contents, index and bibliography header, which are still in roman font.
主要是由于polyglossia一个bug所产生的,解决方案是使用babel来替换polyglossia。或者重定义
captionsenglish, captiongerman。
链接盒子太高 报错讯息

The border around borders around links, as created by hyperref is too high. This looks bad. 这个错误主要是用于拉丁现代字体导致的,计算机现代字体相对好一些。如图: TeX&LaTeX宏包冲突集锦
David Jones分析了这一问题。结果是拉丁现代字体的字母上半部分和下半部分比计算机现代字体要高一些。不幸的是,Xdvipdfmx引擎总会计算字体的上半部分和下半部分的盒子高度,而不是字体的线高度或字形高度。 因此这一问题的解决方案是使用不用的引擎或者使用其他字体来解决。如果没有可选择的,可以修改xdvipdfmx的源代码。 XeTeX 0.996, fontspec and Latin Modern Error: Random characters (garbled text) when using the fontspec package. First of all, make sure you specified fonts for all font families: roman, serif and monospaced: \usepackage{fontspec}   % OpenType/TrueType font support. Requires XeTeX. % CMU = Computer Modern Unicode, seehttps://cm-unicode.sourceforge.net/ \defaultfontfeatures{Mapping=tex-text} \setmainfont{CMU Serif} \setsansfont{CMU Sans Serif} \setmonofont{CMU Typewriter Text} Latin Modern is an extension to the older Computer Modern font set, and is the default font in the fontspec package. However, the naming convention of Latin Modern (lm) changed around the time TexLive 2007 came out. If you use Latin Modern with TexLive 2007, you will get the random characters in the output file. In order to fix this, either use another font, such as Computer Modern Unicode (CMU) or upgrade to a newer version of XeTeX and the fontspec package. Enable Small Capitals with Fontspec Error: Latin Modern does not have small capitals. (\textsc{Small Capitals} does not work). If you use XeTeX and fontspec, you need to specify which font XeTeX uses for small capitals. For Latin Modern Roman, that is Lation Modern Roman Caps, and for Latin Modern Mono, that is Latin Modern Mono Caps. Latin Modern Sans does not have a small capital variant. \usepackage{fontspec} \setmainfont[SmallCapsFont={* Caps}]{Latin Modern Roman} \setsansfont{Latin Modern Sans}  % There is no small caps variant \setmonofont[SmallCapsFont={Latin Modern Mono Caps}]{Latin Modern Mono Light} Bold Typewriter is Not Bold Error: \texttt{Typewriter \textbf{Bold}} gives normal (not bold) text. The standard LaTeX typewriter (Computer Modern) font does not have a distinguishable bold variant. Neither does the replacement font Latin Modern. See LaTeX Bold Typewriter Font how to solve this. Output File Removed If you run XeTeX, and no PDF file is created and the output gives the notice: Output file removed. If you see this, you can get more details by splitting the run process in two: xelatex --no-pdf test xdvipdfmx test.xdv or (depending on your default driver): xelatex --no-pdf test xdv2pdf test.xdv The xdvipdfmx or xdv2pdf driver typically gives a more detailed error message. (seenext entry.) Failed to Find Font Error: xdv2pdf <Error>: can't find font object for font id 2949602. xdv2pdf <Error>: WARNING: Type1 font data returned by OFAStreamPSDownload isn't in the correct format required by the Adobe Type 1Font Format specification. xdv2pdf <Error>: ATSFontGetPostScriptName failed: error -984. ttf_create_with_id: failed to find font object for id 2949602. xdv2pdf <Error>: WARNING: --font-2949602-6--: missing or invalid font descriptor. All these errors are caused by xdv2pdf, which is a relative old driver for XeTeX. You are recommended to use the xdvipdfmx driver, which is the default since XeTeX 0.999.7 (present in TexLive 2008 or later). Alternatively, specify the driver explicitly (though you may need to install it first): xelatex -output-driver=xdvipdfmx test or: xelatex --no-pdf test xdvipdfmx -vv test.xdv Warning: WARNING: Type1 font data returned by OFAStreamPSDownload isn't in the correct format required by the Adobe Type 1Font Format specification. If you only get the above warning, you can safely ignore it, as the output will look good. Thecause of this error is yet unknown. The solution according to the XeTeX site is to use a different Font. However, I got the same problem with all sort of fonts. If you get the other errors about missing fonts, your document willnot look good. While at first sight it may look good, I found that some texts are missing in pictures and formulas. Invalid Font Description Error: ** WARNING ** Found file "/Library/Fonts/Optima.dfont" for PFB font but it doesn't look like a PFB... *** ERROR ** Invalid font: -1 (0) or: ** ERROR ** Cannot proceed without the "native" font: CMUSerif-Roman (CMU Serif Roman)... If you encounter the above errors, first of all, make sure you use a recent version of XeTeX. I encountered to above errors with version 0.999.6, while version 0.999.7 and higher works fine. If you still get the errors with XeTeX 0.999.7 or higher, run xelatex --no-pdf test xdvipdfmx -vv test.xdv And examine the output and log files. Perhaps a font is not found or could not be loaded.