Search code examples
pdf-generationpandocorg-modetex

Org-mode + Pandoc correctly place images in HTML, and misplace them in PDF export


With some org-mode code I am writing, I get the expected images placement when exporting to HTML5 format, but the wrong one when exporting to PDF.

This is the syntax I am using:

#+TITLE: Article title
#+DATE: 2023-02-02
#+AUTHOR: me <me@gmail.com>
#+EMAIL: me@gmail.com
#+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

In HTML5, I get the text and the images in the same sequence as in the org-mode file. In PDF, I get:

** Chapter1 title

Bla bla 1...

bla bla 2:

bla bla 3:

bla bla 4.

** Chapter2 title

plot1.png

plot2.png

plot3.png

To make the conversion, I am using Pandoc:

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

This is the tex conversion output:

% 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 me@gmail.com\textgreater{}},
  pdflang={en},
  hidelinks,
  pdfcreator={LaTeX via pandoc}}

\title{Article title}
\author{me \textless me@gmail.com\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}

The following line should be in charge of setting the figures placement:

\def\fps@figure{htbp}

and among the h, t, b and p parameters, h should have priority, which is the one specifying figures to be placed in the same position as indicated in the source file.

For some reason, this detail is not picked when converting from tex to PDF.


Solution

  • As explained here, add the following file (named, e.g., "disable_float.tex"):

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

    And call Pandoc with:

    pandoc -H ./disable_float.tex -s ./orgtest.org -o ./orgtest.pdf