3

I have asked before, but I never got the answer I was looking for. (old question is here: Defining a new figure environment)

As the title suggests I'm looking for a way to make 2 images stand side by side by defining a new environment, not a command.

The example is the same as the old question: How can I define a new environment, that makes 2 images stand side by side

This is what I have tried without it working.

\documentclass[a4paper,11pt,fleqn,twoside,openright]{memoir}
\usepackage{caption}
\usepackage{graphicx,caption}
\usepackage{float}

\newenvironment{sidebyside}[6]{
    \begin{figure}[H]
        \centering
            \begin{subfigure}[b]{0.49\textwidth}
            \includegraphics[width=\textwidth]{Lists/Images/#1}
            \caption{#2}
        \end{subfigure}
        %
        \begin{subfigure}[b]{0.49\textwidth}
            \includegraphics[width=\textwidth]{Lists/Images/#3}
            \caption{#4}
        \end{subfigure}
        \caption{\protect\raggedright #5}
        \label{#6}
    \end{figure}
}

The sole reason I want it as an environment, is because I'm working with people who doesn't understand LaTeX coding, so I need to make it as easy for them as possible.

Edit for clarification:

When the user types "\begin{sidebyside}" and presses enter, (s)he should see the following code pop up:

\begin{sidebyside}
    \begin{figure}[H]
        \centering
            \begin{subfigure}[b]{0.49\textwidth}
            \includegraphics{}
            \caption{}
        \end{subfigure}
        %
        \begin{subfigure}[b]{0.49\textwidth}
            \includegraphics{}
            \caption{}
        \end{subfigure}
        \caption{\protect\raggedright }
        \label{}
    \end{figure}
\end{sidebyside}

Edit 2:

Apparently my question is still hard to understand. So I've recorded what I'd want to happen here: https://youtu.be/vEBT6Z5TwJQ

EDIT FINAL:

I understand now, that the LaTex editors most people use, obviously isn't like the online solutions. This question is based on my inexperience with the editors, and was rooted in the online editor Overleaf, which has some luxurious shortcuts and autocompleting commands and environments, which obviously doesn't apply in the 'regular' editors. So I understand now why it was hard to understand.

9
  • Off-topic: Don't you think you might be better of defining a template/snippet with that structure? Good editors usually support some feature of the kind.
    – gusbrs
    Dec 2, 2018 at 14:05
  • How do you expect the user interface to be? You have written it in a way that would fit into a command. I can't imagine who you would want this to look as environment.
    – TeXnician
    Dec 2, 2018 at 14:09
  • @gusbrs If that makes the code "pop-up" like it does when \begin{figure} +enter is written, then yes, it might be the solution I'm looking for.
    – L. Nielsen
    Dec 2, 2018 at 14:21
  • @TeXnician I wouldn't say I'm new to LaTeX, but I still don't know a lot of things, for example what you mean when you say "user interface". Maybe I need to clarify in my post that I want it to "act" in the same way as the basic \begin{figure}-environment when I write \begin{sidebyside} +[Enter] I don't know how else to describe it.
    – L. Nielsen
    Dec 2, 2018 at 14:24
  • With user interface I do not refer to a TeXnical term, but to a general technical one. What is the user expected to do/see? What should be the content of \begin{sidebyside}...\end{sidebyside}? In my view, the usual setup with so many fixed variables would be a command…
    – TeXnician
    Dec 2, 2018 at 14:31

1 Answer 1

7

Here's an environment-based solution.

enter image description here

\documentclass[a4paper,11pt,fleqn,twoside,openright]{memoir}
\usepackage[demo]{graphicx} % remove 'demo' option in real document
\usepackage{subcaption,float}

\newenvironment{sidebyside}[6]{%
   \begin{figure}[H]
   \begin{subfigure}[b]{0.49\textwidth}
      \includegraphics[width=\textwidth]{Lists/Images/#1}
      \caption{#2}
   \end{subfigure}\hspace{\fill}%
   \begin{subfigure}[b]{0.49\textwidth}
      \includegraphics[width=\textwidth]{Lists/Images/#3}
      \caption{#4}
   \end{subfigure}
   \caption{\protect\raggedright #5}
   \label{#6}}{%
   \end{figure}}

\begin{document}
\setcounter{chapter}{2} % just for this example
\begin{sidebyside}%
  {pic1}{Caption of first subfigure}%
  {pic2}{Caption of second subfigure}%
  {Overall caption}{fig:x}
\end{sidebyside}
\end{document}

That said, I agree with @TeXnician that a macro-based solution seems far more natural and at least as straightforward to implement. First the command definition:

\newcommand{\sidebyside}[6]{%
   \begin{figure}[H]
   \begin{subfigure}[b]{0.49\textwidth}
      \includegraphics[width=\textwidth]{Lists/Images/#1}
      \caption{#2}
   \end{subfigure}\hspace{\fill}%
   \begin{subfigure}[b]{0.49\textwidth}
      \includegraphics[width=\textwidth]{Lists/Images/#3}
      \caption{#4}
   \end{subfigure}
   \caption{\protect\raggedright #5}
   \label{#6}
   \end{figure}}

Second, a way to call the command:

\sidebyside{pic1}{Caption of first subfigure}%
           {pic2}{Caption of second subfigure}%
           {Overall caption}{fig:x}
5
  • This works fine for me, because I know how to use it, but my colleagues doesn't know what to write in the different { }, no matter how many times I tell them. Do you any way to make it like I've described in my post-edit?
    – L. Nielsen
    Dec 2, 2018 at 15:03
  • 2
    @L.Nielsen - Your follow-up question would appear to be highly editor-dependent. I'm afraid I am utterly unfamiliar with Overleaf. Maybe post a query on the Overleaf help site for advice on generating a pop-up window?
    – Mico
    Dec 2, 2018 at 15:10
  • Ah, I see my question is hard to understand still... I'm not talking about pop-up windows :/..... You know how the \begin{figure} environment folds out? I have made a video, showing what I want to happen: youtu.be/vEBT6Z5TwJQ . Please read the description in the video, as I can't make annotations anymore...
    – L. Nielsen
    Dec 2, 2018 at 16:32
  • @L.Nielsen - Your original query was, "I'm looking for a way to make 2 images stand side by side by defining a new ENVIRONMENT, not a command". That's the query I tried to answer. This site really only works well if each query has one, and only one, substantive question. If you have a substantially new follow-up question, you should post a new, separate query. Finally, I continue to be utterly unfamiliar with Overleaf. If you choose to post a new query, you should make clear that any answers must be Overleaf-specific.
    – Mico
    Dec 2, 2018 at 16:45
  • Yes, and I appreciate the answer and it works. Thank you. I will be more specific in the future.
    – L. Nielsen
    Dec 2, 2018 at 16:51

You must log in to answer this question.

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