Org-mode + Pandoc在HTML中正确放置图像,但在PDF导出中放置图像不正确。

huangapple go评论42阅读模式
英文:

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

问题

这是一段关于使用org-mode代码导出到HTML5和PDF格式时,图片位置不符预期的情况的描述。在HTML5格式中,图片和文本按照org-mode文件中的顺序显示,但在PDF中,图片和文本的顺序不一致。使用Pandoc进行转换,代码中提到了Tex文件的一部分,但没有提供解决方案或问题的明确陈述。

英文:

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.

答案1

得分: 0

如下所述1,请添加以下文件(例如命名为“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
英文:

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

huangapple
  • 本文由 发表于 2023年3月7日 19:41:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661528.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定