实验室手册的纯 TeX 图像解决方法

实验室手册的纯 TeX 图像解决方法

我正在将用 TeX 编写的物理实验室手册导入 Overleaf,以便我们的教师可以协作并更新内容。

一切都运行正常,除了插入图片。图片是使用命令添加的\figure\midfigure\figure\topfigure最初是在 Tex 中,但 Overleaf 无法渲染 .eps 图片(无论编译器和 latexmkrc 设置如何)。

我发现的 \figure 和 \midfigure 的定义如下:

\def\figure#1,#2x#3.{\par\vbox to#2bp{\vss
    \special{psfile="figures/#1" voffset=0 hoffset=#3 vscale=100 hscale=100}}}
\def\figure#1"#2" #3 to #4, #5.{\global\advance\fignum by1 #1
    \oldpicheight=#3bp
    \picheight=#4
    \scaling=\picheight
    \multiply\scaling by100
    \divide\scaling by\oldpicheight
    \vbox to#4{\vss\special{psfile="../figures/#2" voffset=0 hoffset=#5 hscale=\number\scaling vscale=\number\scaling\ }}
    \ifx\relax\thecaption\else{\narrower\narrower\noindent\thecaption\par}\fi
    \endinsert
    \global\let\thecaption=\relax}

% Syntax: \topfig"name.jpg" <height>[in pixels] to <height>[on page], <hoffset> "caption"
\def\topfig{\figure\topinsert} % \topfig "filename" {old height in points} to {new height}, {hoffset}.
\def\midfig{\figure\midinsert}
\def\pagefig{\figure\pageinsert}

我发现的解决方法是通过以下方式使用 LaTeX 包:

\input miniltx.tex \input graphicx.sty

并将图像代码替换为如下内容:

\includegraphics[height=4.5in,width=5.5in]{figures/Titlepicture.eps}

现在所有图片都显示出来,但不幸的是,其中一些图片需要水平偏移,我不知道如何添加。我尝试像本教程中那样合并图形环境(https://www.overleaf.com/learn/latex/Inserting_Images),但没有成功。除了无法添加水平偏移外,新代码运行良好。我尝试了和的变体\centerline,但\centering没有成功。谢谢!

在 Overleaf 上,我使用的是带有 TeX live 版本 2020(旧版)的 LaTex 编译器。原始代码显示它是用 XeTex 编写的。这是我的 latexmkrc 文件:

#$latex = 'tex %O %S'; # to use Knuth's original TeX engine
#$latex = 'pdftex %O %S'; # to use the pdfTeX engine
#$latex = 'luatex %O %S'; # to use the LuaTeX engine
$latex = 'xetex %O %S';  # to use the XeTeX engine

答案1

第一个\figure定义可能被覆盖了,所以没有用处。

需要更新的是第二个\figure。由于存在一些依赖关系,我不知道我自己没有运行测试样本,但这个宏应该与原始宏兼容。

\def\figure#1"#2" #3 to #4, #5.{\global\advance\fignum by1 #1
    \oldpicheight=#3bp
    \picheight=#4
    \scaling=\picheight
    \divide\scaling by\oldpicheight
    \vbox to #4{\vss\leavevmode\kern#5pt\includegraphics[scale=\number\scaling]{../figures/#2}}
    \ifx\relax\thecaption\else{\narrower\narrower\noindent\thecaption\par}\fi
    \endinsert
    \global\let\thecaption=\relax}

% Syntax: \topfig"name.jpg" <height>[in pixels] to <height>[on page], <hoffset> "caption"

但是如果您想摆脱旧的宏(我认为您应该这样做),您可以参考我之前的回答。


上一个答案:

看来手册需要编译 XeTeX,并且\figure似乎是一个定制的宏。

图形未水平对齐的问题是由于 graphicx图像本身未居中对齐。您可以使用 来解决\centerline{#1}此问题,其本质上与 类似\hbox{\hfil#1\hfil}

我的解决方法是重新定义\figure使用包。我通过纯粹猜测使用普通 TeX 的方法和使用方法以及基于我在 pdfTeX 中插入图形的方法来graphicx实现宏。如果您不需要标题,您可以调整第三个参数,如果不打算调整图片大小,则可以将其包含在宏中。下面的示例在 Overleaf 上使用 XeTeX 进行了测试,尽管我没有专门针对 EPS 进行测试,但它应该可以工作。\midfigure\midinsert\topfigure\topinsert\includegraphics

latexmkrc:

$latex = 'xetex %O %S';
\input graphicx

\def\figure#1#2#3{%
\edef\instr{\string#1}
\edef\midfigstr{\string\midfigure}
\edef\topfigstr{\string\topfigure}
\ifx\instr\midfigstr
\midinsert
\else \ifx\instr\topfigstr
\topinsert
\else \errmessage{What is #1?} \fi\fi
\centerline{#2}\par
\centerline{#3}\endinsert}
This is good.
Text Text$\ldots$

\figure\midfigure{\includegraphics{a.png}}{Figure 1: this is an image.}

\vfil\eject %new page

This is good.
Text Text$\ldots$
\figure\topfigure{\includegraphics{a.png}}{Ok.}
\bye

第一页 (\midfigure首页 第二页 ( \topfigure) 第二页

相关内容