我正在使用 pandoc(版本 2.19.2)编写文档。我想使用ruled
浮动样式,类似于自动应用于pandoc-crossref
代码块(转换为codelisting
浮点数)的样式。float
软件包文档说使用这些命令将此样式应用于图形和表格:
\floatstyle{ruled}
\restylefloat{figure}
\restylefloat{table}
但是,一旦我将这些命令添加到我的模板中,所有子图引用都会比它们应该的低一个。
下面是一个例子(image.png
只是一个占位符- 任何图像都可以):
---
subfigGrid: true
lof: true
---
\floatstyle{ruled}
\restylefloat{figure}
\restylefloat{table}
# My first chapter
This is an intro chapter. It references [@fig:example1], [@fig:example2a] and also [@fig:example2b].
![Image 1](image.png){#fig:example1}
<div id="fig:example2">
![Image 2a](image.png){#fig:example2a width=45%}
![Image 2b](image.png){#fig:example2b width=45%}
Example with subfigures
</div>
这是构建命令:
pandoc --filter=pandoc-crossref --citeproc --standalone --pdf-engine=xelatex -o sample.pdf sample.md
以下是渲染的输出:
- 图表列表显示了正确的数字
- 字幕显示正确的数字
- 文中的参考文献没有:
请注意,它说的是fig. 1.1a
和,而fig. 1.1b
应该说的是1.2a
和1.2b
。
\restylefloat
我认为命令和参考计数器之间存在一些冲突pandoccrossref
,但我不知道如何解决这个问题。
我如何使用这种样式并保持参考编号正确?
更新:这是生成的 TeX 输出(使用pandoc --filter=pandoc-crossref --citeproc --standalone -o sample.tex sample.md
% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
]{article}
\usepackage{amsmath,amssymb}
\usepackage{lmodern}
\usepackage{iftex}
\ifPDFTeX
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
\usepackage{unicode-math}
\defaultfontfeatures{Scale=MatchLowercase}
\defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
\usepackage[]{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
\KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\usepackage{longtable,booktabs,array}
\usepackage{calc} % for calculating minipage widths
% Correct order of tables after \paragraph or \subparagraph
\usepackage{etoolbox}
\makeatletter
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
\makeatother
% Allow footnotes in longtable head/foot
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
\makesavenoteenv{longtable}
\usepackage{graphicx}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
% Set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{-\maxdimen} % remove section numbering
\makeatletter
\@ifpackageloaded{subfig}{}{\usepackage{subfig}}
\@ifpackageloaded{caption}{}{\usepackage{caption}}
\captionsetup[subfloat]{margin=0.5em}
\AtBeginDocument{%
\renewcommand*\figurename{Figure}
\renewcommand*\tablename{Table}
}
\AtBeginDocument{%
\renewcommand*\listfigurename{List of Figures}
\renewcommand*\listtablename{List of Tables}
}
\newcounter{pandoccrossref@subfigures@footnote@counter}
\newenvironment{pandoccrossrefsubfigures}{%
\setcounter{pandoccrossref@subfigures@footnote@counter}{0}
\begin{figure}\centering%
\gdef\global@pandoccrossref@subfigures@footnotes{}%
\DeclareRobustCommand{\footnote}[1]{\footnotemark%
\stepcounter{pandoccrossref@subfigures@footnote@counter}%
\ifx\global@pandoccrossref@subfigures@footnotes\empty%
\gdef\global@pandoccrossref@subfigures@footnotes{{##1}}%
\else%
\g@addto@macro\global@pandoccrossref@subfigures@footnotes{, {##1}}%
\fi}}%
{\end{figure}%
\addtocounter{footnote}{-\value{pandoccrossref@subfigures@footnote@counter}}
\@for\f:=\global@pandoccrossref@subfigures@footnotes\do{\stepcounter{footnote}\footnotetext{\f}}%
\gdef\global@pandoccrossref@subfigures@footnotes{}}
\@ifpackageloaded{float}{}{\usepackage{float}}
\floatstyle{ruled}
\@ifundefined{c@chapter}{\newfloat{codelisting}{h}{lop}}{\newfloat{codelisting}{h}{lop}[chapter]}
\floatname{codelisting}{Listing}
\newcommand*\listoflistings{\listof{codelisting}{List of Listings}}
\makeatother
\ifLuaTeX
\usepackage{selnolig} % disable illegal ligatures
\fi
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\urlstyle{same} % disable monospaced font for URLs
\hypersetup{
hidelinks,
pdfcreator={LaTeX via pandoc}}
\author{}
\date{}
\begin{document}
\listoffigures
\floatstyle{ruled}
\restylefloat{figure}
\restylefloat{table}
\hypertarget{my-first-chapter}{%
\section{My first chapter}\label{my-first-chapter}}
This is an intro chapter. It references fig.~\ref{fig:example1},
fig.~\ref{fig:example2a} and also fig.~\ref{fig:example2b}.
\begin{figure}
\hypertarget{fig:example1}{%
\centering
\includegraphics{image.png}
\caption{Image 1}\label{fig:example1}
}
\end{figure}
\begin{pandoccrossrefsubfigures}
\subfloat[Image
2a]{\includegraphics[width=0.45\textwidth,height=\textheight]{image.png}\label{fig:example2a}}
\subfloat[Image
2b]{\includegraphics[width=0.45\textwidth,height=\textheight]{image.png}\label{fig:example2b}}
\caption[{Example with subfigures}]{Example with subfigures}
\label{fig:example2}
\end{pandoccrossrefsubfigures}
\end{document}
更新 2:我发现\restylefloat*
该命令的版本(不移动标题)不会导致这个编号问题,但我需要弄清楚如何处理标题。
答案1
参考计数器似乎没有问题,相反,和包pandoccrossref
之间似乎存在不兼容,因为该文档导致相同的不正确行为:float
subfig
\documentclass{article}
\usepackage{float,subfig,graphicx}
\floatstyle{ruled}
\restylefloat{figure}
\begin{document}
\section{My first chapter}\label{my-first-chapter}
This is an intro chapter. It references fig.~\ref{fig:example1},
fig.~\ref{fig:example2a} and also fig.~\ref{fig:example2b}.
\begin{figure}[H]
\centering
\includegraphics[width=0.45\textwidth]{example-image-a}
\caption{Image 1}\label{fig:example1}
\end{figure}
\begin{figure}[H]
\centering
\subfloat[Image 2a]{\includegraphics[width=0.45\textwidth]{example-image-b}\label{fig:example2a}}
\subfloat[Image 2b]{\includegraphics[width=0.45\textwidth]{example-image-b}\label{fig:example2b}}
\caption[{Example with subfigures}]{Example with subfigures}
\label{fig:example2}
\end{figure}
\end{document}
当我\usepackage{float,subfig,graphicx}
用\usepackage{float,subcaption,graphicx}
结果替换时,结果是正确的,因此您可以创建一个subfig.sty
包含内容的本地文件
\ProvidesPackage{subfig}[2023/03/20 v1.0 subfig -> subcaption]
\expandafter\let\csname [email protected]\endcsname\@undefined % needed for older versions of subcaption package
\RequirePackageWithOptions{subcaption}
\endinput
作为一种(非常)快速而肮脏的解决方法。
答案2
维护者pandoc-crossref
项目给了我一个解决方法用于\captionsetup[figure]{position=bottom}
将子图标题移到图下方。这修复了图形计数:
---
subfigGrid: true
lof: true
header-includes: |
\AtEndPreamble{%
\floatstyle{ruled}
\restylefloat{figure}
\restylefloat{table}
\captionsetup[figure]{position=bottom}
}
---
# My first chapter
This is an intro chapter. It references [@fig:example1], [@fig:example2a] and also [@fig:example2b].
![Image 1](image.png){#fig:example1}
<div id="fig:example2">
![Image 2a](image.png){#fig:example2a width=45%}
![Image 2b](image.png){#fig:example2b width=45%}
Example with subfigures
</div>
生成以下 TeX 输出:
% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
]{article}
\usepackage{amsmath,amssymb}
\usepackage{lmodern}
\usepackage{iftex}
\ifPDFTeX
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
\usepackage{unicode-math}
\defaultfontfeatures{Scale=MatchLowercase}
\defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
\usepackage[]{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
\KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\usepackage{longtable,booktabs,array}
\usepackage{calc} % for calculating minipage widths
% Correct order of tables after \paragraph or \subparagraph
\usepackage{etoolbox}
\makeatletter
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
\makeatother
% Allow footnotes in longtable head/foot
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
\makesavenoteenv{longtable}
\usepackage{graphicx}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
% Set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{-\maxdimen} % remove section numbering
\AtEndPreamble{%
\floatstyle{ruled}
\restylefloat{figure}
\restylefloat{table}
\captionsetup[figure]{position=bottom}
}
\makeatletter
\@ifpackageloaded{subfig}{}{\usepackage{subfig}}
\@ifpackageloaded{caption}{}{\usepackage{caption}}
\captionsetup[subfloat]{margin=0.5em}
\AtBeginDocument{%
\renewcommand*\figurename{Figure}
\renewcommand*\tablename{Table}
}
\AtBeginDocument{%
\renewcommand*\listfigurename{List of Figures}
\renewcommand*\listtablename{List of Tables}
}
\newcounter{pandoccrossref@subfigures@footnote@counter}
\newenvironment{pandoccrossrefsubfigures}{%
\setcounter{pandoccrossref@subfigures@footnote@counter}{0}
\begin{figure}\centering%
\gdef\global@pandoccrossref@subfigures@footnotes{}%
\DeclareRobustCommand{\footnote}[1]{\footnotemark%
\stepcounter{pandoccrossref@subfigures@footnote@counter}%
\ifx\global@pandoccrossref@subfigures@footnotes\empty%
\gdef\global@pandoccrossref@subfigures@footnotes{{##1}}%
\else%
\g@addto@macro\global@pandoccrossref@subfigures@footnotes{, {##1}}%
\fi}}%
{\end{figure}%
\addtocounter{footnote}{-\value{pandoccrossref@subfigures@footnote@counter}}
\@for\f:=\global@pandoccrossref@subfigures@footnotes\do{\stepcounter{footnote}\footnotetext{\f}}%
\gdef\global@pandoccrossref@subfigures@footnotes{}}
\@ifpackageloaded{float}{}{\usepackage{float}}
\floatstyle{ruled}
\@ifundefined{c@chapter}{\newfloat{codelisting}{h}{lop}}{\newfloat{codelisting}{h}{lop}[chapter]}
\floatname{codelisting}{Listing}
\newcommand*\listoflistings{\listof{codelisting}{List of Listings}}
\makeatother
\ifLuaTeX
\usepackage{selnolig} % disable illegal ligatures
\fi
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\urlstyle{same} % disable monospaced font for URLs
\hypersetup{
hidelinks,
pdfcreator={LaTeX via pandoc}}
\author{}
\date{}
\begin{document}
\listoffigures
\hypertarget{my-first-chapter}{%
\section{My first chapter}\label{my-first-chapter}}
This is an intro chapter. It references fig.~\ref{fig:example1},
fig.~\ref{fig:example2a} and also fig.~\ref{fig:example2b}.
\begin{figure}
\hypertarget{fig:example1}{%
\centering
\includegraphics{image.png}
\caption{Image 1}\label{fig:example1}
}
\end{figure}
\begin{pandoccrossrefsubfigures}
\subfloat[Image
2a]{\includegraphics[width=0.45\textwidth,height=\textheight]{image.png}\label{fig:example2a}}
\subfloat[Image
2b]{\includegraphics[width=0.45\textwidth,height=\textheight]{image.png}\label{fig:example2b}}
\caption[{Example with subfigures}]{Example with subfigures}
\label{fig:example2}
\end{pandoccrossrefsubfigures}
\end{document}
渲染输出如下: