XiangyunHuang/ElegantBookdown

添加附录后影响📚封面图片位置错乱

Closed this issue · 11 comments

当前状态预览,可以看到封面图片两个并排挤在一起

https://xiangyunhuang.github.io/ElegantBookdown/ElegantBookdown.pdf

代码修改记录 31ab58c

这个地方实在不解,求助 @fyemath

这个是因为LaTeX里有类似下面这样一段代码,将所有的超过页面宽度一半的图片都缩小了。

\expandafter\patchcmd\csname Gin@ii\endcsname 
{\setkeys{Gin}{#1}}
{\setkeys{Gin}{max width=0.5\textwidth, max height=.5\textwidth, keepaspectratio,#1}}
{}
{}

把他删掉,或者去掉0.5就好了。如果想保留,并且不影响cover,可以加下面的代码。

%The following codes scale images in mainmatter if necessary
%It will be overwrite by local setting in \includegraphics[width, height, ...]{}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\textwidth \textwidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>0.4\textheight 0.4\textheight\else\Gin@nat@height\fi}
\makeatother

\makeatletter
\ifboolexpr{   test {\@ifclassloaded{book}}
            or test {\@ifclassloaded{elagantbook}}
            or test {\@ifclassloaded{memoir}}}
{\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}}
{
    \if@mainmatter
        \setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
    \fi
}
\makeatother

这两段代码需要下面的packages。

\usepackage[export]{adjustbox} %Needed for max width
\usepackage{etoolbox} %Needed for \pathcmd

这几段代码我好像没有用在elegantbookdown-demo里面。但是用到了maple4calc项目里面。:)

我调用的是 Pandoc's 内建的 LaTeX 模版

https://github.com/jgm/pandoc/blob/e1d302b3ef4369790f0ee127b0fd2f735c1c903a/data/templates/default.latex#L275-L289

\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

并没有上述限制。我在 https://github.com/ElegantLaTeX/ElegantBook/blob/master/elegantbook.cls 里也没找到您说的类似代码限制,我使用的 elegantbook 版本是 3.10.

如果可以的话,非常欢迎您给提交 PR。

这个现象是在我添加附录的时候出现,就是说如果把文件 07-appendix.Rmd 去掉图片位置就不会乱了

P.S.

说实在的我感觉这个问题相当诡异,随 PDF 一起生成的 LaTeX 源文件在 https://github.com/XiangyunHuang/ElegantBookdown/blob/gh-pages/ElegantBookdown.tex

抱歉。我开始没有仔细看。我刚下载了下来,跑了一下。问题就出在pandoc那段代码。只缩小大的,小的不会放大。但是elagantbook预期的是,让小图片也填满宽度。我看看怎么处理。弄好了告诉你。

能方便告诉我你的真名吗?我想把你列为 ElegantBookdown 模版的2号开发者

能方便告诉我你的真名吗?我想把你列为 ElegantBookdown 模版的2号开发者
叶 飞 :)
我尽力。R刚开始接触。LaTeX用的比较久。

我们正好优势互补,我觉得自己 R 还不错, LaTeX 一知半解。

找到办法了。在preamble 加上下面的代码就好了。不是最好的办法,只能算是个破解。最好是pandoc或着elegantbook做些update。

\usepackage[export]{adjustbox} %Needed for max width
\usepackage{etoolbox} %Needed for \pathcmd
%\patchcmd{<command>}{<code to replace>}{<code>}{<success>}{<failure>}
%The following codes add max dimension option to includegraphics
%Gin@ii is from graphicx package and looks for a second optional argument
\expandafter\patchcmd\csname Gin@ii\endcsname 
{\setkeys{Gin}{#1}}
{\setkeys{Gin}{max width=\textwidth, max height=.5\textwidth,keepaspectratio,#1}}
{}
{}

这段代码定义\setkeys{Gin},加上了max width这样的命令。这个命令应该好像会覆盖width。要看看adjustbox的手册,才能确定。

我发现 Pandoc 官网高频问题这里有记录,但是没有搞明白,这里面的逻辑到底是否是合理的?

https://pandoc.org/faqs.html#when-i-specify-an-image-width-of-50-and-convert-to-latex-pandoc-sets-the-height-to-textheight-and-the-aspect-ratio-isnt-preserved-how-can-i-prevent-this

If you don’t have this in your custom template, you should add it. If we didn’t set the height explicitly in this way, the image would not be resized correctly unless it was being resized to smaller than its original size.

LaTeX的\includegraphics局部的长宽设置会覆盖\setkey{Gin}的设置。如果只用了width=50%, 长度会被默认为length=\textwidth. 这样原来的全局设置里面的keepaspectratio也不会起作用了。所以就会出现图片不对的问题。