![根据 DPI 和显示尺寸调整图像大小](https://linux22.com/image/314356/%E6%A0%B9%E6%8D%AE%20DPI%20%E5%92%8C%E6%98%BE%E7%A4%BA%E5%B0%BA%E5%AF%B8%E8%B0%83%E6%95%B4%E5%9B%BE%E5%83%8F%E5%A4%A7%E5%B0%8F.png)
我发现我的 pdf 非常大,因为我在其中使用了许多高质量图片。为了自动降低质量,我发现了这个问题 [pdflatex(或任何 tex 包)可以自动重新缩放已缩小尺寸的包含图像吗?] 使用这个非常酷的答案代码(感谢@daau):
% for downsampling large images: \includegraphicsRS
% (\includegraphics "resize")
\newcommand{\includegraphicsRS}{}
\ifnum\pdfshellescape=1
% Yes, enabled
% #2 is the filename - assumed relative
% IFP - image file path
% IFN - image filename only
% here resizing at 800 px wide
\newread\myinput
% must add default argument [] so typeout works
\renewcommand\includegraphicsRS[2][\@empty]{%
% run downsampling bash script
\immediate\write18{mkdir -p downsampled; IFP=#2 ; %
echo "Processing $IFP" ; %
IFN=$(basename $IFP) ; %
echo "downsampled/$IFN" > tmpname ; %
if [ ! -f downsampled/$IFN ]; then %
echo "File downsampled/$IFN not found! Converting..." ; %
% convert $IFP -resize 50\% downsampled/$IFN ; %
convert $IFP -resize 800x downsampled/$IFN ; %
else %
echo "Found downsampled/$IFN - reusing." ; %
fi ; %
} % end write18
% here we should have downsampled file
% retrieve downsample filename first
\immediate\openin\myinput=tmpname
% The group localizes the change to \endlinechar
\bgroup
\endlinechar=-1
\immediate\read\myinput to \localline
% Since everything in the group is local, we have to explicitly make the
% assignment global
\global\let\myTmpFileName\localline
\egroup
\immediate\closein\myinput
% Clean up after ourselves
% \immediate\write18{rm -f -- tmpname}
% finally, include downsampled image
\includegraphics[#1]{\myTmpFileName}
} % end renewcommand
\else
% No, disabled
% in this case, \includegraphicsRS is just the usual \includegraphics
\renewcommand\includegraphicsRS[2][\@empty]{%
\includegraphics[#1]{#2}
}
\fi
现在我不想简单地对每个图像使用 800px 的宽度,而是根据\includegraphicsRS
(例如\includegraphicsRS[width=0.5\textwidth]{test.jpg}
)的第一个参数计算像素宽度。
为此,我想用 px 中的实际文本宽度替换字符串 \textwidth。
问题 1:我用了
textwidth="\the\textwidth" ; %
TW=${textwidth:0:(-2)} ; %
在\write18
环境中。第一行工作正常(对于边距为 2cm 的 a4,返回类似“455.24411pt”的内容)。
但是第二行(旨在删除末尾的 pt)不起作用。为什么?我不明白,也找不到任何相关内容。
问题2:因为它需要一些更丑陋的步骤(乘以 1.3 之类的数字,找到 xxwidth=xx\textwidth
并计算它)并且之后它仍然只适用于\textwidth
:有没有人有更好的想法如何做到这一点?