我希望用线条绘制列表。但下面的示例输出到两个不同的页面。
\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}
输出:
第 0 页:
第 1 页:
答案1
我希望我正确理解了你的问题。如果你将先验放在环境tikzpicture
中lstlisting
(使其在同一页上),则输出将交错:
\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}
问题是,现在确实lstlisting
开始了一个新段落,并在其前面引入了一些垂直空白,将其中一些移动到了下一页。您可以尝试使用 some 恢复此空间\vspace
,或者如果您可以保证它适合一页,您可以这样\hbox
做:
\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}