LyXSide-by-side subfigures don't work for program listings

Information and discussion about LyX, a WYSIWYM editor, available for Linux, Windows and Mac OS X systems.
Post Reply
jamshark70
Posts: 4
Joined: Thu Jan 15, 2026 9:22 am

Side-by-side subfigures don't work for program listings

Post by jamshark70 »

Here, I have followed the instructions in https://wiki.lyx.org/FAQ/FiguresSideBySide:
  • Float
    • Subfigure (width 0.45\textwidth)
    • Horizontal fill
    • Subfigure (width 0.45\textwidth)
In the page screenshot, the top float places a program listing into the first subfigure and an image into the second, and the program listing's top boundary aligns with the image's bottom boundary. :?: :?:

The lower float has two images and those are horizontally aligned as I expected.
lyx-side-by-side-problem.png
lyx-side-by-side-problem.png (22.84 KiB) Viewed 475 times
https://tex.stackexchange.com/questions ... -in-memoir says "The listings has full width so that will never work. You may need to wrap the listings in a minipage to control the width" but LyX is already wrapping it in a minipage (exported):

Code: Select all

\begin{figure}
\begin{minipage}[t]{0.45\textwidth}%
\begin{lstlisting}
... code here...
\end{lstlisting}
%
\end{minipage}\hfill{}%
\begin{minipage}[t]{0.45\textwidth}%
\includegraphics{../img/02-02-pd-phasor}%
\end{minipage}
I have to admit i really don't understand what is going on here. Why does a child element not obey its parent's bounds?

Unfortunately, in this document, I expect to have many pairs of code listings alongside images; lack of a solution would be a deal-breaker. So, how to fix this?

hjh

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

User avatar
Stefan Kottwitz
Site Admin
Posts: 10397
Joined: Mon Mar 10, 2008 9:44 pm

Re: Side-by-side subfigures don't work for program listings

Post by Stefan Kottwitz »

\includegraphics sets the baseline to the bottom. The baseline of your listing, top, is aligned with the baseline of the image, the bottom. A quick fix is to insert \vspace{0pt} right before the \includegraphics command; this way, the reference point will be the top, and both subfigures will be top-aligned.

Stefan
LaTeX.org admin
jamshark70
Posts: 4
Joined: Thu Jan 15, 2026 9:22 am

Re: Side-by-side subfigures don't work for program listings

Post by jamshark70 »

Stefan Kottwitz wrote: Fri Jan 16, 2026 8:54 pm \includegraphics sets the baseline to the bottom. The baseline of your listing, top, is aligned with the baseline of the image, the bottom.
Ohhh I see. I could never have guessed that, since both minipages are set to top alignment. That's counterintuitive, isn't it? A parent object, a minipage (top alignment), contains a child object, an \includegraphics with implicit bottom alignment -- I would really expect the child object not to be able to override the parent.

But your comment pointed me to another solution -- to set middle alignment. This looks even nicer on the page than top alignment.

(Still undecided whether I will continue with LyX or start writing LaTeX directly.)

hjh
Post Reply