2

Here I found a similar question with horizontal rule, which answer I don't understand to a degree I could use it to solve my problem:

Adding a horizontal rule between caption and lstlisting inside a tcolorbox


I am looking for a separator rule for line numbers when the frame rule has already been extended to include the line numbers (Would be nice if it was possible with just the listings package). See this example:

Listings: recognize numbers and `1e-3` sample


Minimal working example:

\documentclass{report}

\usepackage{xcolor}
\definecolor{halfgray}{gray}{0.55}
\definecolor{ipython_frame}{RGB}{207, 207, 207}

\usepackage{listings}

\lstdefinelanguage{iPython}{
    commentstyle=\color{cyan}\ttfamily,
    stringstyle=\color{red}\ttfamily,
    keepspaces=true,
    showspaces=false,
    showstringspaces=false,
    %
    rulecolor=\color{ipython_frame},
    frame=single,
    frameround={t}{t}{t}{t},
    framexleftmargin=6mm,
    numbers=left,
    numberstyle=\tiny\color{halfgray},
    %
    basicstyle=\scriptsize\ttfamily,
    keywordstyle=\color{green}\ttfamily,
}

\begin{document}
\begin{lstlisting}[language=iPython]
import math
import numpy as np
from lib.analytical import csa

sin2_theta  = np.sin(theta)**2
+= -= *= /= + - * / ? < > & % == <=
# += -= *= /= + - * / ? < > & % == <=
def test(a=100, b=True):
    <= >= == 2 + 3j * 7e-3
\end{lstlisting}
\end{document}
7
  • 2
    Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}.
    – jub0bs
    Commented Mar 30, 2015 at 1:10
  • That is already there! Just see the second linked question Listings: recognize numbers and 1e-3. I added the words "minimal working" so it might become more of an eye-catcher.
    – BadAtLaTeX
    Commented Mar 30, 2015 at 9:31
  • On this site, each question should be more or less standalone and should have its own MWE (when appropriate). Besides, the question you link to contains code that is far from minimal for the present question.
    – jub0bs
    Commented Mar 30, 2015 at 9:36
  • The MWE is not the complete language definition anymore, but just code with some frame.
    – BadAtLaTeX
    Commented Mar 30, 2015 at 9:47
  • That's much better :)
    – jub0bs
    Commented Mar 30, 2015 at 9:57

1 Answer 1

3

A rather hacky solution is to use tcolorbox for the border so that you can use listings frame for the rule. Tcolorbox provides a library for boxes that contain listings and minted codes. Below is a minimal setting but you have lots of room for improvement since tcolorboxes are highly customizable:

\documentclass{report}

\usepackage{xcolor}
\definecolor{halfgray}{gray}{0.55}
\definecolor{ipython_frame}{RGB}{207, 207, 207}
\usepackage{tcolorbox}

\tcbuselibrary{listings}%
\usepackage{lipsum}
\usepackage{listings}
\tcbset{listing engine={listings}}

\lstdefinelanguage[]{iPython}[]{python}{
    commentstyle=\color{cyan}\ttfamily,
    stringstyle=\color{red}\ttfamily,
    keepspaces=true,
    showspaces=false,
    showstringspaces=false,
    %
    rulecolor=\color{ipython_frame},
    frame=l,
    numbers=left,
    numberstyle=\tiny\color{halfgray},
    xleftmargin={0.75cm},
    %
    basicstyle=\scriptsize\ttfamily,
    keywordstyle=\color{green}\ttfamily,
}

\begin{document}
\lipsum[1]
\begin{tcblisting}{boxrule=1pt, listing only,listing options={language=iPython}}
import math
import numpy as np
from lib.analytical import csa

sin2_theta  = np.sin(theta)**2
+= -= *= /= + - * / ? < > & % == <=
# += -= *= /= + - * / ? < > & % == <=
def test(a=100, b=True):
    <= >= == 2 + 3j * 7e-3
\end{tcblisting}
\lipsum[2]
\end{document}

Which will give you this:

enter image description here

A quick note on defining new language in listings: You can use a base dialect for the new language so you don't need to define all the keywords from scratch.

\lstdefinelanguage
   [[hdialecti]]{hlanguagei}
   [[hbase dialecti]{hand base languagei}]
   {hkey=value listi}
   [[hlist of required aspects (keywordcomments,texcs,etc.)]]

hence \lstdefinelanguage[]{iPython}[]{python}.

7
  • This package seems to be either badly documented or corrupted. If I copy the command for "Producing tcblisting Environments" NewTCBListing I am getting an error. -> It has to be lower case newtcblisting. "Producing tcbinputlisting Commands" however is not working at all for me. Then you never really know what code is actually necessary or producing which part of the examples... I am a bit lost searching for the tcbinputlisting.
    – BadAtLaTeX
    Commented Mar 30, 2015 at 20:22
  • Turns out this 'kinda works': \newtcbinputlisting[]{iPython}{ colframe=ipython_frame, colback=ipython_bg, left=0pt, top=-9pt, bottom=-9pt, boxrule=0.4pt, listing only, listing options={language=iPython}, }{} Yields an error though, not being able to find document.listing(.tex) when I try to use \iPython{test.py}. Input is crucial, since it's a good way to keep an oversight of different source codes right after another, imho.
    – BadAtLaTeX
    Commented Mar 30, 2015 at 20:34
  • @hillbilly, sorry, but I'm not sure what are you asking in your comment. Can you elaborate a bit more please?
    – Pouya
    Commented Mar 31, 2015 at 8:12
  • Well, I don't know how to describe it further. See tcolorbox.18.5. I would like to use \newtcbinputlisting to create a lstinputlisting-like command, which yields the above mentioned error though. The final product should be an style-file for iPython that I can reuse with a simple input and environment command.
    – BadAtLaTeX
    Commented Apr 5, 2015 at 13:58
  • @hillbilly, I am not on a computer and it is rather difficult to answer it without a computer. I suggest you to post a new question to get the help of others and perhaps getting an answer quicker. I'll be back in few days and I'll try to answer this. cheers!
    – Pouya
    Commented Apr 5, 2015 at 18:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .