1

I'm currently facing an aesthetic issue. The borders of my code/algorithm section are being divided by in-code math markup:

enter image description here

Table has been generated with the following code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath, amsfonts}
\usepackage{listings}

\title{Title}
\author{Author}
\date{Date}

\begin{document}

\maketitle

\lstset{
numbers=left, 
numberstyle=\small, 
numbersep=8pt, 
frame = single, 
language=Python, 
framexleftmargin=15pt
}

\begin{lstlisting}[escapeinside={(*}{*)}]
if (*$\exists s \in S \subset \mathbb{R}^2 \times \{\text{label } | \text{ label } \in \{0,1\} \}$*) with label 1:
    (*$r = \underset{\text{label(s) = 1} }{\max ||s||}$*)
else:
    (*$r = \underset{\text{label(s) = 0} }{\min ||s||} - 10^{-1000} $*)
return B[0, r]
\end{lstlisting}

\end{document}

Anyone knows how can I make the vertical borders be continuous despite the math markup? Or at least remove the vertical borders?

I am working in Overleaf, by the way.

Thank you!

  • Can you provide the MWE from \documentclass to \end{document}? – MadyYuvi Sep 14 at 13:45
  • @MadyYuvi Edited the question. Plese tell me if I provided enough information. – theSongbird Sep 14 at 13:53
2

You can use tcblisting to generate the frame for you.

Or, to remove vertical borders, simply specify frame=none.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tcolorbox}
\usepackage{amsmath, amssymb}

\tcbuselibrary{listings, breakable, skins}

\title{Title}
\author{Author}
\date{Date}

\newtcblisting{mylisting}{
  enhanced,
  breakable,
  listing only,
  sharp corners,
  arc=0mm,
  colback=white,
  boxsep=1mm,
  top=0mm,
  bottom=0mm,
  left=6mm,
  right=1mm,
  listing options={
    numbers=left, 
    numberstyle=\small, 
    numbersep=8pt, 
    language=Python, 
    escapeinside={(*}{*)}
  }
}

\begin{document}

\maketitle


\begin{mylisting}
if (*$\exists s \in S \subset \mathbb{R}^2 \times \{\text{label } | \text{ label } \in \{0,1\} \}$*) with label 1:
    (*$r = \underset{\text{label(s) = 1} }{\max ||s||}$*)
else:
    (*$r = \underset{\text{label(s) = 0} }{\min ||s||} - 10^{-1000} $*)
return B[0, r]
\end{mylisting}

\end{document}
| improve this answer | |
  • Any way to set the thickness of the border? – theSongbird Sep 14 at 14:43
  • 1
    border thickness can be adjusted with boxrule=1pt in the declaration of mylisting – Alan Xiang Sep 14 at 15:58
1

It was a bug in listings.sty explained in the documentation under the heading 8.2

enter image description here

and the modified MWE is:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{moreverb}
\usepackage{amsmath,amsfonts,listings}
\usepackage{mdframed}
\begin{document}

\lstset{
numbers=left, 
numberstyle=\small, 
numbersep=2pt, 
%frame = single, 
language=Python, 
framexleftmargin=15pt
}

\begin{verbatimwrite}{temp.c}
if (*$\exists s \in S \subset \mathbb{R}^2 \times \{\text{label } | \text{ label } \in \{0,1\} \}$*) with label 1:
    (*$r = \underset{\text{label(s) = 1} }{\max ||s||}$*)
else:
    (*$r = \underset{\text{label(s) = 0} }{\min ||s||} - 10^{-1000} $*)
return B[0, r]
\end{verbatimwrite}

\begin{mdframed}
\lstinputlisting[escapeinside={(*}{*)}]{./temp.c}
\end{mdframed}


\end{document}

Output

enter image description here

| improve this answer | |
  • Any way to minimize top and bottom margin within the frame? – theSongbird Sep 14 at 14:58
  • 1
    It is possible, and you can meet the requirement, with the help of \begin{mdframed}[innertopmargin=12pt,innerbottommargin=12pt] – MadyYuvi Sep 15 at 5:56

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.