在 Latex 中包含两个单独的图形作为子图

在 Latex 中包含两个单独的图形作为子图

我在 tex 文件中有两个单独的图形:例如 fig1.tex 和 fig2.tex

\begin{figure}[t]
\begin{tikzpicture}
.
.
.
\end{tikzpicture}
\caption{caption}
\label{fig1}
\end{figure}

我使用\input{fig1.tex}\input{fig2.tex}将它们main.tex作为两个单独的图形包括在内

如何将它们作为子图包含在一个图中?

答案1

诀窍是暂时重新定义figuresubfigure

\begin{filecontents}{fig1}
\begin{figure}[t]
\begin{tikzpicture}
\node{Hello world!};
\end{tikzpicture}
\caption{caption}
    \label{fig1}
\end{figure}
\end{filecontents}
%
\begin{filecontents}{fig2}
\begin{figure}[t]
\includegraphics{photo}
\caption{caption}
    \label{fig2}
\end{figure}
\end{filecontents}
%
\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{subcaption}

\begin{document}
\begin{figure}[t]
\bgroup
  \renewenvironment{figure}[1][]{\subfigure{\dimexpr 0.5\textwidth-0.5\columnsep}}{\endsubfigure}
  \input{fig1}\hfill
  \input{fig2}
\egroup
\end{figure}


\end{document}

演示

答案2

不太清楚您想要什么。可能是这样的?

在此处输入图片描述

这可以通过使用小页面来轻松实现,例如,您可以tikzpicture使用\input{<file name>}和 以同样的方式插入一些图像(但是,在这种情况下,使用 直接插入更简单includegraphics)。当然,每个小页面也应该包含\caption\label

\begin{filecontents}{fig-a}
\begin{tikzpicture}
\node{Hello world!};
\end{tikzpicture}
\end{filecontents}

\begin{filecontents}{fig-b}
\includegraphics{photo}
\end{filecontents}


\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}

\begin{document}
\begin{figure}[ht]
    \begin{minipage}[b]{0.45\linewidth}
\input{fig-a}%\includegraphics[width=\linewidth]{example-image-duck}
\caption{caption}
    \label{fig-a}
    \end{minipage}\hfill\begin{minipage}[b]{0.45\linewidth}
                          \input{fig-b}
                          \caption{caption}
                          \label{fig-b}
                         \end{minipage}
\end{figure}
See figure \ref{fig-a} and \ref{fig-b} ...

\end{document}

相关内容