将方程式转换为图像

将方程式转换为图像

我想将 LaTeX 方程式转换为pdf输出中的图像。经过搜索,我找到了一些可以转换的网站和软件来源个人方程式转换为图像,但如果我可以在整个tex文件上运行这样的转换器并让其自动将方程式转换为图像,那就太好了。手动转换每个方程式并将其作为图像插入文档中非常繁琐。

\subsection{Linear equation through a point given its gradient}
A very simple differential equation is to find a linear equation through a particular point $P(x_i,y_i)$ given its gradient $k_i$ (or more commonly used for lines, its slope).

\begin{equation}
\frac{dy}{dx} = k_i \iff \int dy = \int k_i \cdot dx \iff y = k_i \cdot x + C
\end{equation}

Plug in the initial conditions to find the particular value of the constant of integration $C$.

这是方程式包含可选文本的默认输出(在 Acrobat Reader 中):

当前 pdf 输出:公式包含文本

但我想知道是否有可能输出一个pdf将方程式转换为这样的图像(在 Acrobat Reader 中):

所需的 PDF 输出:将公式打印为图像

注:最初,我问了一个不同的问题但后来我决定大幅度改写。结果,评论和答案实际上与那个旧问题不再相关。我决定将修订后的版本重新发布为新问题。

答案1

一种可能性是将每个方程式放在一个里面tikzpicture,然后使用tikzexternalize自动将每个方程式转换为 png 并包含该 png,类似于tikz 外部默认使用 png 而不是 pdf。下面的实现需要您\begin{equation}\end{equation}用新命令替换\pngequation

这需要用 进行编译-shell-escape。请注意,创建图像的编译器是硬编码的,在本例中是pdflatex,如果您想要其他编译器,可以对其进行修改。

请注意,这只是一个概念证明。它相当迂回,很可能存在限制、问题和/或副作用,因此不推荐 - 对于大多数用例,会有更简单的解决方案,如评论中所述。

梅威瑟:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize

\tikzset{%
    % Add size information to the .dpth file (png is in density not size)
    /pgf/images/external info,
    % Use the png export AND the import
    use png/.style={png export,png import},
    png export/.style={
        external/system call=%
        {pdflatex \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode -jobname "\image" "\texsource";%
         convert -density 300 -transparent white "\image.pdf" "\image.png"; rm -f "\image.pdf"},
    },
    png import/.code={%
        \tikzset{%
            /pgf/images/include external/.code={%
                % Here you can alter to whatever you want
                % \pgfexternalwidth is only available if /pgf/images/external info
                % is set
                \includegraphics%
                [width=\pgfexternalwidth,height=\pgfexternalheight]%
                {{##1}.png}%
            }%
        }%
    }%
}
\newcounter{pngeq}
\setcounter{pngeq}{0}
\newcommand{\pngequation}[1]{%
\tikzset{use png}
\begin{tikzpicture}
\node (eq) {
\hspace{-1.1\parindent}\begin{minipage}{\textwidth}%
\setcounter{equation}{\number\value{pngeq}}
\begin{equation}
#1
\end{equation}
\end{minipage}
};
\end{tikzpicture}%
\stepcounter{pngeq}
}

\begin{document}
\section{Linear equation through a point given its gradient}
A very simple differential equation is to find a linear equation through a particular point $P(x_i,y_i)$ given its gradient $k_i$ (or more commonly used for lines, its slope).

\pngequation{
\frac{dy}{dx} = k_i \iff \int dy = \int k_i \cdot dx \iff y = k_i \cdot x + C
}

Plug in the initial conditions to find the particular value of the constant of integration $C$.


\pngequation{
\frac{dy}{dx} = k_i \iff \int dy = \int k_i \cdot dx \iff y = k_i \cdot x + C'
}
\end{document}

结果:

在此处输入图片描述


对于子方程,您可以按照相同的思路扩展上面的解决方案。首先添加一个使用主计数器重置的额外子方程计数器:

\newcounter{pngsubeq}[pngeq]

然后定义一个命令\pngsubequation来步进这个计数器并在编号中使用它。

\newcommand{\pngsubequation}[1]{%
\tikzset{use png}
\ifnum\value{pngsubeq}=0\stepcounter{pngeq}\fi% first subequation, step main counter
\stepcounter{pngsubeq}
\begin{tikzpicture}
\node (eq) {
\hspace{-1.1\parindent}\begin{minipage}{\textwidth}%
\renewcommand{\theequation}{\arabic{pngeq}\alph{pngsubeq}}
\begin{equation}
#1
\end{equation}
\end{minipage}
};
\end{tikzpicture}%
}

为了对子方程的主计数器进行计数,可以向subequations环境中添加一个钩子:

\BeforeBeginEnvironment{subequations}{\stepcounter{pngeq}}

然后你就可以\begin{subequations}\pngsubeqation里面使用:

\begin{subequations}
\pngsubequation{...}

\pngsubequation{...}
\end{subequations}

完整代码,带有 feq 组正规方程和子方程:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize

\tikzset{%
    % Add size information to the .dpth file (png is in density not size)
    /pgf/images/external info,
    % Use the png export AND the import
    use png/.style={png export,png import},
    png export/.style={
        external/system call=%
        {pdflatex \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode -jobname "\image" "\texsource";%
         convert -density 300 -transparent white "\image.pdf" "\image.png"; rm -f "\image.pdf"},
    },
    png import/.code={%
        \tikzset{%
            /pgf/images/include external/.code={%
                % Here you can alter to whatever you want
                % \pgfexternalwidth is only available if /pgf/images/external info
                % is set
                \includegraphics%
                [width=\pgfexternalwidth,height=\pgfexternalheight]%
                {{##1}.png}%
            }%
        }%
    }%
}
\newcounter{pngeq}
\setcounter{pngeq}{0}
\newcounter{pngsubeq}[pngeq]
\newcommand{\pngequation}[1]{%
\tikzset{use png}
\stepcounter{pngeq}
\begin{tikzpicture}
\node (eq) {
\hspace{-1.1\parindent}\begin{minipage}{\textwidth}%
\renewcommand{\theequation}{\arabic{pngeq}}
\begin{equation}
#1
\end{equation}
\end{minipage}
};
\end{tikzpicture}%
}
\BeforeBeginEnvironment{subequations}{\stepcounter{pngeq}}
\newcommand{\pngsubequation}[1]{%
\tikzset{use png}
\stepcounter{pngsubeq}
\begin{tikzpicture}
\node (eq) {
\hspace{-1.1\parindent}\begin{minipage}{\textwidth}%
\renewcommand{\theequation}{\arabic{pngeq}\alph{pngsubeq}}
\begin{equation}
#1
\end{equation}
\end{minipage}
};
\end{tikzpicture}%
}


\begin{document}
\section{Linear equation through a point given its gradient}
A very simple differential equation is to find a linear equation through a particular point $P(x_i,y_i)$ given its gradient $k_i$.

\pngequation{
\frac{dy}{dx} = k_i \iff \int dy = \int k_i \cdot dx \iff y = k_i \cdot x + C
}

Plug in the initial conditions to find the particular value of the constant of integration $C$.


\pngequation{
\frac{dy}{dx} = k_i \iff \int dy = \int k_i \cdot dx \iff y = k_i \cdot x + C'
}

A very simple differential equation is to find a linear equation through a particular point $P(x_i,y_i)$ given its gradient $k_i$.

\begin{subequations}
\pngsubequation{
\frac{dy}{dx} = k_i \iff \int dy = \int k_i \cdot dx \iff y = k_i \cdot x + C
}

Plug in the initial conditions to find the particular value of the constant of integration $C$.

\pngsubequation{
\frac{dy}{dx} = k_i \iff \int dy = \int k_i \cdot dx \iff y = k_i \cdot x + C'
}
\end{subequations}

A very simple differential equation is to find a linear equation through a particular point $P(x_i,y_i)$ given its gradient $k_i$.

\pngequation{
\frac{dy}{dx} = k_i \iff \int dy = \int k_i \cdot dx \iff y = k_i \cdot x + C
}

Plug in the initial conditions to find the particular value of the constant of integration $C$.


\pngequation{
\frac{dy}{dx} = k_i \iff \int dy = \int k_i \cdot dx \iff y = k_i \cdot x + C'
}

A very simple differential equation is to find a linear equation through a particular point $P(x_i,y_i)$ given its gradient $k_i$.

\begin{subequations}
\pngsubequation{
\frac{dy}{dx} = k_i \iff \int dy = \int k_i \cdot dx \iff y = k_i \cdot x + C
}

Plug in the initial conditions to find the particular value of the constant of integration $C$.

\pngsubequation{
\frac{dy}{dx} = k_i \iff \int dy = \int k_i \cdot dx \iff y = k_i \cdot x + C'
}
\end{subequations}
\end{document}

\tikzset{use png}
\begin{tikzpicture}
\node (eq) {
\hspace{-\parindent}\begin{minipage}{\textwidth}%
\begin{equation}
\frac{dy}{dx} = k_i \iff \int dy = \int k_i \cdot dx \iff y = k_i \cdot x + C
\end{equation}
\end{minipage}
};
\end{tikzpicture}

结果:

在此处输入图片描述

相关内容