1

I wish to draw over listings with lines. but below sample output into 2 different page.

\documentclass{article}
\usepackage[left=10mm,right=2mm,top=2mm,bottom=2mm,paperwidth=12cm,paperheight=4cm,layoutwidth=12cm,layoutheight=4cm]{geometry}
\usepackage{xcolor}
\usepackage{tikz}
\usepackage{listings}
\usetikzlibrary{tikzmark}
\usetikzmarklibrary{listings}
\definecolor{back-color}{rgb}{0.1686, 0.1686, 0.1686}
\definecolor{string-color}{rgb}{0.3333, 0.5254, 0.345}
\definecolor{key-color}{rgb}{0.8, 0.47, 0.196}
\lstdefinestyle{mystyle}{
    language = C++,
    basicstyle = {\ttfamily },
    backgroundcolor = {},
    stringstyle = {\color{string-color}},
    morekeywords = {iostream},
    keywordstyle = {\color{key-color}},
    frame=Trbl,numbers=left,
}
\makeatletter
\global\lst@linemarktrue
\makeatother
\begin{document}
\begin{lstlisting}[style=mystyle,name=lst1]
#include <iostream>
using namespace std;
int x = 2;
//comment
for (int i = 0; i < x; ++i) {
    cout << "stand_alone_complex" << endl;
}
\end{lstlisting}
\begin{tikzpicture}[remember picture,overlay]
\tikzset{dot/.style={circle,fill=red,minimum size=4pt,inner sep=0}}
\foreach \i in {1,...,7} {
    \coordinate (PS\i) at (pic cs:line-lst1-\i-start);
    \coordinate (PE\i) at (pic cs:line-lst1-\i-end);
    \draw (PS\i) -- (PE\i);
}
\end{tikzpicture}
\end{document}

Output:

Page 0:

enter image description here

Page 1:

enter image description here

1

I hope I understood your question correctly. If you put the tikzpicture prior to the lstlisting environment (such that it is on the same page) the output becomes interleaved:

\documentclass{article}
\usepackage[left=10mm,right=2mm,top=2mm,bottom=2mm,paperwidth=12cm,paperheight=4cm,layoutwidth=12cm,layoutheight=4cm]{geometry}
\usepackage{xcolor}
\usepackage{tikz}
\usepackage{listings}
\usetikzlibrary{tikzmark}
\usetikzmarklibrary{listings}
\definecolor{back-color}{rgb}{0.1686, 0.1686, 0.1686}
\definecolor{string-color}{rgb}{0.3333, 0.5254, 0.345}
\definecolor{key-color}{rgb}{0.8, 0.47, 0.196}
\lstdefinestyle{mystyle}{
    language = C++,
    basicstyle = {\ttfamily },
    backgroundcolor = {},
    stringstyle = {\color{string-color}},
    morekeywords = {iostream},
    keywordstyle = {\color{key-color}},
    frame=Trbl,numbers=left,
}
\makeatletter
\global\lst@linemarktrue
\makeatother
\begin{document}
\begin{tikzpicture}[remember picture,overlay]
\tikzset{dot/.style={circle,fill=red,minimum size=4pt,inner sep=0}}
\foreach \i in {1,...,7} {
    \coordinate (PS\i) at (pic cs:line-lst1-\i-start);
    \coordinate (PE\i) at (pic cs:line-lst1-\i-end);
    \draw (PS\i) -- (PE\i);
}
\end{tikzpicture}%
\begin{lstlisting}[style=mystyle,name=lst1]
#include <iostream>
using namespace std;
int x = 2;
//comment
for (int i = 0; i < x; ++i) {
    cout << "stand_alone_complex" << endl;
}
\end{lstlisting}
\end{document}

The problem is that now the lstlisting does start a new paragraph and introduces some vertical white space before it, shifting some of it onto the next page. You could either try to revert this space using some \vspace, or if you can guarantee that it'll fit on one page, you could just \hbox it:

\documentclass{article}
\usepackage[left=10mm,right=2mm,top=2mm,bottom=2mm,paperwidth=12cm,paperheight=4cm,layoutwidth=12cm,layoutheight=4cm]{geometry}
\usepackage{xcolor}
\usepackage{tikz}
\usepackage{listings}
\usetikzlibrary{tikzmark}
\usetikzmarklibrary{listings}
\definecolor{back-color}{rgb}{0.1686, 0.1686, 0.1686}
\definecolor{string-color}{rgb}{0.3333, 0.5254, 0.345}
\definecolor{key-color}{rgb}{0.8, 0.47, 0.196}
\lstdefinestyle{mystyle}{
    language = C++,
    basicstyle = {\ttfamily },
    backgroundcolor = {},
    stringstyle = {\color{string-color}},
    morekeywords = {iostream},
    keywordstyle = {\color{key-color}},
    frame=Trbl,numbers=left,
}
\makeatletter
\global\lst@linemarktrue
\makeatother

\begin{document}
\hbox{%
\begin{tikzpicture}[remember picture,overlay]
\tikzset{dot/.style={circle,fill=red,minimum size=4pt,inner sep=0}}
\foreach \i in {1,...,7} {
    \coordinate (PS\i) at (pic cs:line-lst1-\i-start);
    \coordinate (PE\i) at (pic cs:line-lst1-\i-end);
    \draw (PS\i) -- (PE\i);
}
\end{tikzpicture}%
\begin{lstlisting}[style=mystyle,name=lst1]
#include <iostream>
using namespace std;
int x = 2;
//comment
for (int i = 0; i < x; ++i) {
    cout << "stand_alone_complex" << endl;
}
\end{lstlisting}
}%
\end{document}

enter image description here

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.