LoginSignup
10

More than 5 years have passed since last update.

listings中で単語の切れ目以外の場所でも自動改行させる

Posted at

listings.styを使ってコードブロックを記述する場合、長い行を改行させることが問題になります。

長い行を改行させる設定は以下のようになります。

\lstset{
  breaklines=true,
  (...)
}

しかし、この設定だけでは、「 」や「+」などの文字がある場合はその辺りで改行してくれるのですが、英数字だけが長く続く場合には改行してくれません。

単語中など、どんな文字の並びでも文字数を超えた時には改行させる、ということを実現するには、以下のような設定を\begin{document}の前に記述します。

\makeatletter
\def\lst@lettertrue{\let\lst@ifletter\iffalse}
\makeatother
(...)
\begin{document}

これで、ちょうど文字幅に収まるように改行されます。

参考: http://tex.stackexchange.com/questions/65743/listings-package-does-not-break

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10