1

I have two separate figures in tex file: fig1.tex and fig2.tex for example

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

I use \input{fig1.tex} and \input{fig2.tex} to include them in main.tex as two separate figures

How to include them in one figure as subplots?

1
  • BTW, this is an extremely inefficient way to do things. Try using standalone to test/develop new images and add the captions in the main document. Sep 25, 2023 at 12:38

2 Answers 2

1

The trick is to temporarily redefine figure to be subfigure.

\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}

demo

3
  • thank you for your answer but the figures are not generated although no errors appears Sep 25, 2023 at 11:20
  • I had \textwidth misspelled, but other than than it works fine for me. Is photo not supposed to be a Simpson? Sep 25, 2023 at 12:11
  • I changed it to \begin{tikzpicture} instead of \includegraphics{photo} and i would like to have them at the same horizontal Sep 25, 2023 at 12:16
0

It is not entirely clear what you after. Probably something like this?

enter image description here

This can be simply to obtain by use of minipages, in which you insert for example tikzpicture by use of \input{<file name>} and some image on the same way (however, in such cases is simpler to insert it directly by using includegraphics). Of course, each minipage should contain \caption and \label too:

\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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .