First, put the code you want to display in 2 files (say a and b). Then do the following:
\begin{figure}
\subfloat[subcaption 1]{\lstinputlisting{a}}
\hspace{20pt}
\subfloat[subcaption 2]{\lstinputlisting{b}}
\caption{Global caption}
\end{figure}
This way you obtain the listing side by side. The only issue is that their vertical align is centered. So if the height of the two listings is not the same, the short one is not vertically aligned to the long one.
One way to solve this issue is to use \raisebox{xxpt}{\subfloat[...]{...}} where xx is how much points you want to raise the listing. Anyway I found the result not good enough.
Googling a little bit I found something on the faq section of subfig'sdocumentation. Here it is:
I tried (not that hard to be honest), but I wasn't able to align both figure to the top. After all I think I'll keep the center valign...
6.1 "My sub-floats are not aligned along their bottoms. Why?"
Remember! The subfig package aligns sub-floats along their baselines with the sub- caption (if any) sticking out above or below. The above problem is usually due to using a minipage, tabular or array environment that, by default, places the baseline at the center of the box that it generates. If the two sub-floats are different sizes, or if onesub-float is generated in some other way with its baseline not at the expected place (perhaps an \includegraphics), then the sub-float will be misaligned. One solution is to use the environment options `[t]' or `[b]' to move the baseline to the top or bottom rather than the center.
4 comments:
You can solve the problem of the alignment of the two subfigures using option 'boxpos' of listings. It would be something like:
\subfloat[...]{\lstinputlisting[boxpos=b]{...}}
Great tip, renato! Thanks!
With tables, just use
\begin{tabular}[b]{ccc}
for each table. The [b] will force them to all be at the bottom, or alternatively [t] for at the top.
Thanks for the post, very useful!
I ran into a problem though with listings that have a background color. I'm using a figure* environment (i'm in a 2col style) and the first listing in the subfloat takes up the entire page, instead of just the left column. The second listing in the subfloat is also a page wide and spills over the right side of the page (although it correctly starts on the left at the middle of the page). I simply removed the color and the frame from my listings to sweep the problem under the rug. Don't know how to solve it though.
Post a Comment