0

I want to wrap listing inside a figure. But when I do that there is plent white space in between its caption. How can I remove extra white space of Figure's caption when listing wrap inside a figure?

I have following code, origin:

\expandafter\def\csname ver@fixltx2e.sty\endcsname{}
\documentclass[10pt,journal,compsoc]{IEEEtran}
\PassOptionsToPackage{table}{xcolor}
\ifCLASSOPTIONcompsoc\usepackage[nocompress]{cite}
\else
    \usepackage{cite}
\fi
\ifCLASSINFOpdf\else\fi
\hyphenation{op-tical net-works semi-conduc-tor}
\usepackage[english]{babel}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{listings, xcolor}

\begin{document} \section{Introduction} hello world hello world hello world hello world \begin{figure}[htp] \lstinputlisting[frame=tlrb, rulecolor=\color{black}]{hello.sol} \caption{hello world.} \end{figure} hello world hello world hello world hello world \end{document}

hello.sol:

function hello() public returns (bool) {
    uint256 amount = 100
    return amount * 100
    return true;
}

output:

enter image description here

Here you can see that font of the caption is smaller and there is plenty white space generated on top and bottom of the caption. Is it possible to remove those?

When I have caption inside listing if I can replace Listing with Figure it works for me as well.

alper
  • 1,389

1 Answers1

1

Your MWE shouldn't rely on external files. If possible, please provide a standalone code that can be copy-pasted.

However, a possible solution is to use the caption feature of listings, instead of relying on a figure. Remove the figure environment as well as the \caption command and then:

\lstinputlisting[caption={hello world.}, captionpos=b, frame=tlrb, rulecolor=\color{black}]{hello.sol}
Miyase
  • 2,544
  • 2
  • 12
  • 26
  • No problem, and thanks for the update. Does my answer above provide a satisfactory result? – Miyase May 08 '22 at 11:13
  • The tag is Listing is it possible to replace it as Figure? and have it bottom of the figure instead of top of it – alper May 08 '22 at 11:16
  • The listings documentation mentions the captionpos option. I updated my answer accordingly. – Miyase May 08 '22 at 11:18
  • As for replacing "listings" with "figure", I find it a bit strange to name "figure" something that isn't a figure... But, again according to listings documentation, you can just redefine \lstlistingname for this. – Miyase May 08 '22 at 11:24
  • You are right :-) In the paper I was referring in Figure~\ref{_listing} but I should do in Listing instead seems like its the right way to do it – alper May 08 '22 at 11:26
  • I don't use it myself, but you might want to have a look at package cleveref which handles that sort of detail automatically. – Miyase May 08 '22 at 11:28
  • Can also [ht] applied to listing? When the listing is botto of the page it kind of splitted half to other column, where [ht] of the figure was preventing that – alper May 08 '22 at 11:43
  • Sorry I removed my last comment because I misread your answer. The solution is given page 29 of the documentation, option float. – Miyase May 08 '22 at 12:19