# How to center image in 2 column tabular when one side is Verbatim?

I am having hard time finding the correct way to typeset this: I need to have code as verbatim on one side of the page, and show the output (an image) on the right side of the page.

I have tried using minipage, but that did not work well. I am now trying using a table with 2 columns. I put the verbatim text in the left column, then the image in the right column. The problem is that the alignment is not coming out right. I tried all the tricks I know about which are \raisebox{-.5\height} or using m for column specification.

Here is the output from a MWE:

I'd like it to show up as follows (This was drawn manually using cut/paste) with the help of Visio and paint.exe

Here is the code (using demo option for Graphics so it works as is)

\documentclass[11pt,notitlepage]{article}%
\usepackage{verbatim}
\usepackage{fancyvrb}
\usepackage[demo]{graphicx}
\usepackage{epstopdf}%
\usepackage[margin={2cm}]{geometry}%
\usepackage{array}%for m option

\begin{document}

\begin{tabular}[c]{|p{4in}|p{3in}|}\hline
%   \begin{tabular}{|p{4in}|m{3in}|}\hline    %also tried this, same problem
\begin{Verbatim}[fontsize=\small]
Clear["Global*"];
SetDirectory[NotebookDirectory[]]
sys = TransferFunctionModel[1/(s^2 + 2/10 s + 1), s];

yStep = Assuming[t > 0,
Simplify@OutputResponse[sys, UnitStep[t], t]];
yImpulse = Simplify@OutputResponse[sys,DiracDelta[t],t];
p = Grid[{
{Plot[Evaluate@{yStep, yImpulse}, {t, 0, 50},
PlotRange -> {{0, 50}, {-0.8, 2.0}},
Frame -> True,
FrameLabel -> {{"y(t)", None},
{t, "step and impulse reponse"}},
GridLines -> Automatic,
GridLinesStyle -> Dashed,
ImageSize -> {300, 300},
PlotStyle -> {Red, Black},
AspectRatio -> 1]
},
{
Row[{"step response=", Re@yStep}]
},
{Row[{"impulse response=", Re@yImpulse}]}
}, Alignment -> Left, Frame -> True]

yStep

{1 - E^(-t/10) Cos[(3 Sqrt[11] t)/10]
- (
E^(-t/10) Sin[(3 Sqrt[11] t)/10])/(3 Sqrt[11])}

yImpulse

{(10 E^(-t/10) HeavisideTheta[t]
Sin[(3 Sqrt[11] t)/10])/(3 Sqrt[11])}
\end{Verbatim}
&
\raisebox{-.5\height}{\includegraphics[width=3in,height=3in]{whatever}}
\\\hline
\end{tabular}

\end{document}


TexLive 2013, Linux

• If you use \raisebox{-\height}{...} instead of \raisebox{-.5\height}{...} it seems to work better. – Peter Grill May 20 '14 at 23:04
• @PeterGrill I tried your suggestion, I do not see any difference? screen shot !Mathematica graphics (using Miktex on windows now) \raisebox{-\height}{\includegraphics[width=3in,height=3in]{whatever}} – Nasser May 20 '14 at 23:06
• And which is the desired alignment? – Gonzalo Medina May 20 '14 at 23:10
• @GonzaloMedina Ok, will make a quick sketch, please give me a second – Nasser May 20 '14 at 23:11
• Did you try \begin{tabular}[c]{|m{4in}|m{3in}|} without the \raisebox? This gives you vertically centred objects. – Gonzalo Medina May 20 '14 at 23:11

You can use the m{<length>} column type for both columns:

\documentclass[11pt,notitlepage]{article}%
\usepackage{verbatim}
\usepackage{fancyvrb}
\usepackage[demo]{graphicx}
\usepackage{epstopdf}%
\usepackage[margin={2cm}]{geometry}%
\usepackage{array}%for m option

\begin{document}

\begin{tabular}[c]{|m{4in}|m{3in}|}\hline
\begin{Verbatim}[fontsize=\small]
Clear["Global*"];
SetDirectory[NotebookDirectory[]]
sys = TransferFunctionModel[1/(s^2 + 2/10 s + 1), s];

yStep = Assuming[t > 0,
Simplify@OutputResponse[sys, UnitStep[t], t]];
yImpulse = Simplify@OutputResponse[sys,DiracDelta[t],t];
p = Grid[{
{Plot[Evaluate@{yStep, yImpulse}, {t, 0, 50},
PlotRange -> {{0, 50}, {-0.8, 2.0}},
Frame -> True,
FrameLabel -> {{"y(t)", None},
{t, "step and impulse reponse"}},
GridLines -> Automatic,
GridLinesStyle -> Dashed,
ImageSize -> {300, 300},
PlotStyle -> {Red, Black},
AspectRatio -> 1]
},
{
Row[{"step response=", Re@yStep}]
},
{Row[{"impulse response=", Re@yImpulse}]}
}, Alignment -> Left, Frame -> True]

yStep

{1 - E^(-t/10) Cos[(3 Sqrt[11] t)/10]
- (
E^(-t/10) Sin[(3 Sqrt[11] t)/10])/(3 Sqrt[11])}

yImpulse

{(10 E^(-t/10) HeavisideTheta[t]
Sin[(3 Sqrt[11] t)/10])/(3 Sqrt[11])}
\end{Verbatim}
&
\includegraphics[width=3in,height=3in]{whatever}
\\\hline
\end{tabular}

\end{document}


Fine tuning (if required) can be achieved with a \raisebox.

• Thanks! I had no idea one must use m for both columns, examples I saw just used 'm' for the column which has the image. Is this because one side is Verbatim in this case? Sometimes, I will have normal text in the left side as well (mised with some Verbatim). Will that still work? Will try it. I just did. Yes, it seems to work with normal text as well! This is great. Thank you – Nasser May 20 '14 at 23:17
• @Nasser Yes, as you noticed yourself, it works with regular text as well. – Gonzalo Medina May 20 '14 at 23:32

Here's a solution using the verbatimbox package. While I left the columns as p type, one could use c type just as easily.

\documentclass[11pt,notitlepage]{article}%
%\usepackage{verbatim}
\usepackage{verbatimbox}
\usepackage[demo]{graphicx}
\usepackage{epstopdf}%
\usepackage[margin={2cm}]{geometry}%
\usepackage{array}%for m option

\begin{document}

\begin{verbbox}[\small]
Clear["Global*"];
SetDirectory[NotebookDirectory[]]
sys = TransferFunctionModel[1/(s^2 + 2/10 s + 1), s];

yStep = Assuming[t > 0,
Simplify@OutputResponse[sys, UnitStep[t], t]];
yImpulse = Simplify@OutputResponse[sys,DiracDelta[t],t];
p = Grid[{
{Plot[Evaluate@{yStep, yImpulse}, {t, 0, 50},
PlotRange -> {{0, 50}, {-0.8, 2.0}},
Frame -> True,
FrameLabel -> {{"y(t)", None},
{t, "step and impulse reponse"}},
GridLines -> Automatic,
GridLinesStyle -> Dashed,
ImageSize -> {300, 300},
PlotStyle -> {Red, Black},
AspectRatio -> 1]
},
{
Row[{"step response=", Re@yStep}]
},
{Row[{"impulse response=", Re@yImpulse}]}
}, Alignment -> Left, Frame -> True]

yStep

{1 - E^(-t/10) Cos[(3 Sqrt[11] t)/10]
- (
E^(-t/10) Sin[(3 Sqrt[11] t)/10])/(3 Sqrt[11])}

yImpulse

{(10 E^(-t/10) HeavisideTheta[t]
Sin[(3 Sqrt[11] t)/10])/(3 Sqrt[11])}
\end{verbbox}
\begin{tabular}[c]{|p{4in}|p{3in}|}\hline
\theverbbox&
\raisebox{\dimexpr.5\ht\savedverbbox-.5\height}{\includegraphics[width=3in,height=3in]{whatever}}
\\\hline
\end{tabular}

\end{document}
`

• Thanks, interesting solution, I did not know about verbatimbox package. – Nasser May 21 '14 at 1:40