Org-mode + Pandoc 在 HTML 中正确放置图像,并在 PDF 导出中错放它们

问题描述 投票:0回答:1

使用我正在编写的一些 org-mode 代码,我在导出为 HTML5 格式时得到了预期的图像位置,但在导出为 PDF 时得到了错误的位置。

这是我使用的语法:

#+TITLE: Article title
#+DATE: 2023-02-02
#+AUTHOR: me <[email protected]>
#+EMAIL: [email protected]
#+LANGUAGE: en
#+OPTIONS: toc:2
#+LaTeX_HEADER: \author{me}
#+HTML_DOCTYPE: html5


** Chapter1 title

Bla bla 1...

#+CAPTION: Image caption
#+NAME:   fig:plot1
[[./MyArticle_files/plot1.png]]

bla bla 2:

#+NAME:   fig:plot2
[[./MyArticle_files/plot2.png]]

bla bla 3:

#+NAME:   fig:plot3
[[./MyArticle_files/plot3.png]]

bla bla 4.

** Chapter2 title

在 HTML5 中,我按照与 org-mode 文件中相同的顺序获取文本和图像。在 PDF 中,我得到:

** Chapter1 title

Bla bla 1...

bla bla 2:

bla bla 3:

bla bla 4.

** Chapter2 title

plot1.png

plot2.png

plot3.png

为了进行转换,我正在使用Pandoc

pandoc -s ./text.org -t html5 -o ./text.html
pandoc -s ./text.org -o ./text.tex
pandoc -s ./text.org -o ./text.pdf

这是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{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
\ifLuaTeX
\usepackage[bidi=basic]{babel}
\else
\usepackage[bidi=default]{babel}
\fi
\babelprovide[main,import]{english}
% get rid of language-specific shorthands (see #6817):
\let\LanguageShortHands\languageshorthands
\def\languageshorthands#1{}
\author{me}
\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{
  pdftitle={Article title},
  pdfauthor={me \textless [email protected]\textgreater{}},
  pdflang={en},
  hidelinks,
  pdfcreator={LaTeX via pandoc}}

\title{Article title}
\author{me \textless [email protected]\textgreater{}}
\date{2023-02-02}

\begin{document}
\maketitle

\hypertarget{chapter1-title}{%
\subsection{Chapter1 title}\label{chapter1-title}}

Bla bla 1\ldots{}

\begin{figure}
\centering
\includegraphics{./MyArticle_files/plot1.png}
\caption{Image caption}
\end{figure}

bla bla 2:

\begin{figure}
\centering
\includegraphics{./MyArticle_files/plot2.png}
\caption{}
\end{figure}

bla bla 3:

\begin{figure}
\centering
\includegraphics{./MyArticle_files/plot3.png}
\caption{}
\end{figure}

bla bla 4.

\hypertarget{chapter2-title}{%
\subsection{Chapter2 title}\label{chapter2-title}}

\end{document}

下一行负责设置图形位置:

\def\fps@figure{htbp}

h
,
t
,
b
p
参数中,
h
应该具有优先权,它指定要放置在与源文件中指示的相同位置的数字。

出于某种原因,在从

tex
转换为 PDF 时未选择此细节。

pdf-generation pandoc org-mode tex
1个回答
0
投票

here所述,添加以下文件(命名为“disable_float.tex”):

\usepackage{float}
\let\origfigure\figure
\let\endorigfigure\endfigure
\renewenvironment{figure}[1][2] {
    \expandafter\origfigure\expandafter[H]
} {
    \endorigfigure
}

然后用以下方式调用 Pandoc:

pandoc -H ./disable_float.tex -s ./orgtest.org -o ./orgtest.pdf
© www.soinside.com 2019 - 2024. All rights reserved.