2

I want to embed code in table like the below photo.

enter image description here

Firstly, I tried code 1.

\begin{tabular}{p{8cm}}
\hline
\begin{lstlisting}
code here ......
\end{lstlisting}
\\ \hline
\begin{lstlisting}
code here ......
\end{lstlisting}
\\ \hline
\end{tabular}

However, there is too much space between hline and code.

Then, I tried answer in Remove top space from lstlisting inside tabular.

The space became less than first step. However, there is still much space.

If I want the effect just like picture above, how can I do?

Great thanks!!

1

You have options aboveskip and belowskip. If we set these values to zero, there is still a small space. We can get rid of it by setting negative values.

\documentclass{article}
\usepackage{listings}
\begin{document}
\begin{tabular}{p{8cm}}
\hline
\begin{lstlisting}[aboveskip=-8pt,belowskip=-12pt]
code here ......
\end{lstlisting}
\\ \hline
\begin{lstlisting}[aboveskip=-8pt,belowskip=-12pt]
code here ......
\end{lstlisting}
\\ \hline
\end{tabular}
\end{document}

enter image description here

  • Yes, you are right. I found the solution that is similar to yours. Great thanks~ – Young Apr 25 at 1:37

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.