# Vertically Align Captions of Listings inside Table

I have the following problem: The LaTeX code below produces the output in the picture.

\usepackage{listings, tabularx, caption}

\begin{table}[h]
\centering
\caption{Vergleich von C++ und Swift Code}
\begin{tabularx}{\linewidth}{ | X | X | }

\hline
\lstinputlisting[style=table, caption={C++ Code}]{code/hello_world.cpp} & \lstinputlisting[style=table, caption={C Code}]{code/hello_world.c}\\
\hline

\end{tabularx}\par
\bigskip
Caption
\end{table}


Is there a way to vertically align the captions of the two code examples?

• It would be a lot easier if your captions were not part of the \lstinputlisting. Could you create a listing that consisted of just the caption? – John Kormylo Jan 10 '17 at 14:34

As you are presenting the listings within a table, you could start a new line for the subcaptions.

I personally think, you are using to many captions. In your example you have a caption above the table, subcaptions beneath the listings and another (useless) caption below the table. In my example, I removed the last one.

\documentclass[a4paper,11pt]{article}
\usepackage[main=ngerman]{babel}
\usepackage{listings, tabularx}
\begin{document}

\begin{table}[h]
\caption{Vergleich von C++ und Swift Code}
\begin{tabularx}{\linewidth}{ | X | X | }
\hline
\lstinputlisting{code/hello_world.cpp}
& \lstinputlisting{code/hello_world.c}\\
%% NEW: the following line contains the requested subcaptions,
%% being set on one line.
\parbox{\linewidth}{\centering C++-Beispiel}
& \parbox{\linewidth}{\centering C-Beispiel} \\
\hline
\end{tabularx}
\end{table}
\end{document}


And the outcome: