How to define caption width in longtable?

The error is most likely due to a wrongly place \captionsetup.

I doubt the usefulness of landscape and longtable but here's an example:

\documentclass{article}

\usepackage{longtable}
\usepackage{lscape}
\usepackage{caption}


\usepackage{blindtext}

\begin{document}

\captionsetup{width=5cm}
\begin{landscape}
\begin{longtable}{lll}
 \caption{\blindtext} 
\endfirsthead
 table & table & table \tabularnewline
 table & table & table \tabularnewline 
 table & table & table \tabularnewline
 table & table & table \tabularnewline
 table & table & table \tabularnewline 
 table & table & table \tabularnewline
 table & table & table \tabularnewline
 table & table & table \tabularnewline 
 table & table & table \tabularnewline
 table & table & table \tabularnewline
 table & table & table \tabularnewline 
 table & table & table \tabularnewline
 table & table & table \tabularnewline
 table & table & table \tabularnewline 
 table & table & table \tabularnewline
 table & table & table \tabularnewline
 table & table & table \tabularnewline 
 table & table & table \tabularnewline
 table & table & table \tabularnewline
 table & table & table \tabularnewline 
 table & table & table \tabularnewline
 table & table & table \tabularnewline
 table & table & table \tabularnewline 
 table & table & table \tabularnewline
 table & table & table \tabularnewline
 table & table & table \tabularnewline 
 table & table & table \tabularnewline
 table & table & table \tabularnewline
 table & table & table \tabularnewline 
 table & table & table \tabularnewline
 table & table & table \tabularnewline
 table & table & table \tabularnewline 
 table & table & table \tabularnewline
\end{longtable}
\end{landscape}
\end{document}

enter image description here


As indicated in the documents of longtable package. You can

Set the width of the caption with a command such as \setlength{\LTcapwidth}{2in} in the preamble of your document.

For example, to fit the caption length to table width (equals to textwidth), you can do

\setlength{\LTcapwidth}{\textwidth}

Then it should be work.