1

I want most compact code inside a lstlisting frame. I have tried to fit the frame into the two-column using xleftmargin=1.1mm,xrightmargin=2.5pt by manually trying and checking with eye.

But my real problem is I want to change the line breaks for each sentences to fit more words into it.

Please note that I have used \usepackage{newtxtext,newtxmath}, code is taken from answer for How to change the font style and size for the \lstinputlisting with some changes:

\documentclass[10pt,journal,compsoc]{IEEEtran}
\usepackage{amsmath,amssymb}
\usepackage{lipsum}
\usepackage{newtxtext,newtxmath}
\usepackage{listings}
\usepackage{lstautogobble}
\lstdefinestyle{mystyle}
{
    basicstyle=\ttfamily,
    frame=single,
    breaklines,
    columns=fullflexible,
    breakindent=1.2em,
    breakatwhitespace,
    escapeinside={(*}{*)},
}
\begin{document}
\begin{lstlisting}[style=mystyle,autogobble,xleftmargin=1.1mm,xrightmargin=2.5pt]
    function hello_world(*\,*)(uint a, uint b, uint c, uint d, uint e, uint f) public returns bool {
        uint256 amount = 100
        return true;
    }
\end{lstlisting}
\lipsum*[2]
\end{document}

Output:

enter image description here

For example here, I want to have following as the first line:

function hello_world(uint a, uint b, uint c, uint

where shifting uint to the first line, hence I believe it will still fit into the frame. I have tried:

function hello_world(*\,*)(uint a,(*\,*)uint b,(*\,*)uint c,(*\,*)uint

but it did not helped.

The first reason I have used

alper
  • 1,389

1 Answers1

1

I'm not sure what the problem is, but in general I find nxtt (the default typewriter font used with NewTX) not particularly attractive and too big:

enter image description here

Usually typewriter type is a bit smaller then Roman type.

You can do it with some trickery: use the nott option and define later the same font family, but scaled at 90%.

\documentclass[10pt,journal,compsoc]{IEEEtran}
\usepackage{amsmath,amssymb}
\usepackage{lipsum}
\usepackage[nott]{newtxtext}
\usepackage{newtxmath}
\usepackage{listings}
\usepackage{lstautogobble}

\makeatletter \AtBeginDocument{% \providecommand{\ntx@scaled}{}% \renewcommand{\ntx@scaled}{s*[0.9]}% \renewcommand{\ttdefault}{ntxtt}% } \makeatother

\lstdefinestyle{mystyle} { basicstyle=\ttfamily, frame=single, breaklines, columns=fullflexible, breakindent=1.2em, breakatwhitespace, escapeinside={(}{)}, } \begin{document}

abc\texttt{abc}

\begin{lstlisting}[style=mystyle,autogobble,xleftmargin=1.1mm,xrightmargin=2.5pt] function hello_world(,)(uint a, uint b, uint c, uint d, uint e, uint f) public returns bool { uint256 amount = 100 return true; } \end{lstlisting}

\lipsum*[2]

\end{document}

enter image description here

Alternatively, use an altogether different typewriter font.

\documentclass[10pt,journal,compsoc]{IEEEtran}
\usepackage{amsmath,amssymb}
\usepackage{lipsum}
\usepackage[nott]{newtxtext}
\usepackage{newtxmath}
\usepackage[scale=0.95]{zi4}
\usepackage{listings}
\usepackage{lstautogobble}

\lstdefinestyle{mystyle} { basicstyle=\ttfamily, frame=single, breaklines, columns=fullflexible, breakindent=1.2em, breakatwhitespace, escapeinside={(}{)}, } \begin{document} abc\texttt{abc}

\begin{lstlisting}[style=mystyle,autogobble,xleftmargin=1.1mm,xrightmargin=2.5pt] function hello_world(,)(uint a, uint b, uint c, uint d, uint e, uint f) public returns bool { uint256 amount = 100 return true; } \end{lstlisting} \lipsum*[2] \end{document}

enter image description here

Last, but not least, you can decide to reduce the fontsize for a particular listing (or all of them) by setting

basicstyle=\ttfamily\small

or even \footnotesize.

\documentclass[10pt,journal,compsoc]{IEEEtran}
\usepackage{amsmath,amssymb}
\usepackage{lipsum}
\usepackage[nott]{newtxtext}
\usepackage{newtxmath}
\usepackage[scale=0.95]{zi4}
\usepackage{listings}
\usepackage{lstautogobble}

\lstdefinestyle{mystyle} { basicstyle=\ttfamily, frame=single, breaklines, columns=flexible, breakindent=1.2em, breakatwhitespace, escapeinside={(}{)}, } \lstdefinestyle{mystylesmaller} { style=mystyle, basicstyle=\ttfamily\small, }

\begin{document}

abc\texttt{abc}

\begin{lstlisting}[style=mystyle,autogobble,xleftmargin=1.1mm,xrightmargin=2.5pt] function hello_world(,)(uint a, uint b, uint c, uint d, uint e, uint f) public returns bool { uint256 amount = 100 return true; } \end{lstlisting} \begin{lstlisting}[style=mystylesmaller,autogobble,xleftmargin=1.1mm,xrightmargin=2.5pt] function hello_world(,)(uint a, uint b, uint c, uint d, uint e, uint f) public returns bool { uint256 amount = 100 return true; } \end{lstlisting}

\lipsum*[2]

\end{document}

enter image description here

egreg
  • 1,121,712
  • Which one out of the four examples do you recommend to be used? As I understand second one looks more style, right? Is there any difference between lstlisting and lstinputlisting? where can I use lstinputlisting instead ? – alper Oct 03 '22 at 11:09
  • 1
    @alper I like more Inconsolata (zi4); about the font size, you can take a case by case decision. You can use \lstinputlisting when you want to input a file, rather than having the code in the .tex file. – egreg Oct 03 '22 at 12:24
  • Is it possible to reduce all the row's height size little bit? – alper Oct 04 '22 at 14:56
  • 1
    @alper Add \linespread{0.9} in front of \ttfamily – egreg Oct 04 '22 at 17:25