1

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}

enter image description here

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
2

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:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.