
hline
リストがあり、リストの前後に追加したいのですがframes
、スタイル パラメータを使用しても、次の 2 つの理由で意図したとおりに動作しません。1. 番号とソース コードの区切りとして左側のバーを残しておきたい。2. フレームの上部/下部の行と左側の行の間にスペースがない。
サンプルコードは
\documentclass{article}
\usepackage{listings}
\usepackage{lipsum}
\lstdefinestyle{CEE}{language=C, frame=l, numbers=left, numbersep=1em, xleftmargin=2em}
\begin{document}
\lipsum[1]
\begin{lstlisting}[style=CEE, caption={Hello world}]
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Hello world\n");
return 0;
}
\end{lstlisting}
\lipsum[2]
\end{document}
つまり、次のようになります。
しかし、私はフレームの幅だけでなく、\hline
全体を埋める を望みます。(GIMP 編集済み)\textwidth
単に Gimp で編集しただけなので、間隔と線幅が正しくないことに注意してください。アイデアは、table
環境にある間隔と同じです。次のようにします。
答え1
私は行を取得するために パッケージを使用しましたtcolorbox
。環境のオプションは、必要に応じて変更できます。ただし、問題が 1 つあります。とtmpbox
を組み合わせた新しい環境を定義することはできないと思います。そのため、毎回 を明示的に呼び出す必要があります。tmpbox
lstlisting
tmpbox
\documentclass[11pt, a4paper]{article}
\usepackage{geometry}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{tcolorbox}
\tcbuselibrary{skins,breakable}
\newenvironment{tmpbox}{%
\tcolorbox[%
empty,
parbox=false,
noparskip,
enhanced,
breakable,
frame hidden,
boxrule=0pt,
colback=white,
left=-.5ex, % right=-4pt,
before skip=.1ex plus 2pt,
after skip=1ex plus 2pt,
overlay unbroken and last={%
\draw ($(frame.north west)+(0, -6ex)$)
-- +(1\textwidth, 0);
\draw ($(frame.south west)+(0, 2ex)$)
-- +(1\textwidth, 0);
}]
}{\endtcolorbox}
\usepackage{listings}
\lstdefinestyle{CEE}{%
frame=l, language=C, numbers=left, numbersep=1em, xleftmargin=2em
}
\usepackage{lipsum}
\begin{document}
\begin{center}\large\bfseries
Adding horizontal lines about listings
\end{center}
% See \verb|https://en.wikibooks.org/wiki/LaTeX/Source_Code_Listings|.
\lipsum[1]
\begin{tmpbox}
\begin{lstlisting}[style=CEE, caption={Hello world with hlines}]
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Hello world\n");
return 0;
}
\end{lstlisting}
\end{tmpbox}
\lipsum[3]
\begin{lstlisting}[style=CEE, caption={Hello world}]
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Hello world\n");
return 0;
}
\end{lstlisting}
\lipsum[4]
\end{document}