0

I tried to convert a table to longtable but did not succeed.

\documentclass[a4paper,12pt]{report}

\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage[T1]{fontenc}
\usepackage{natbib}
\usepackage[francais]{babel}
\usepackage{url} % Pour écrire des adresses cliquables.
\usepackage{lmodern} % Pour changer le pack de police.
\usepackage[top=2.5cm, bottom=2.5cm, left=2.5cm, right=2.5cm]{geometry}
\usepackage{fancyhdr}
\usepackage{supertabular} % tableaux qui tiennent sur plusieurs pages

\usepackage{array}
\usepackage{pdflscape}
\usepackage[utf8x,utf8]{inputenc}

%\usepackage[numbers]{natbib}
%\usepackage[square]{natbib}
\usepackage{soul} %alignier


\usepackage{epigraph}
\usepackage{setspace}
\usepackage{float}
%\linespread{1.15}
\onehalfspacing
\usepackage{makecell}
\usepackage{tabularx}
\usepackage{amsmath}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{blindtext}
\usepackage{enumitem}
\usepackage{verbatim}
\usepackage{tikz}
\bibliographystyle{abbrvnat1}
%\bibliographystyle{apalike}
\setcitestyle{authordate,open={(},close={)}}


\begin{document}

\begin{table}[H]
    \centering
    \label{CU1-label}
\begin{tabularx}{\textwidth}{|X|}%{|X|}
\hline          
    \textbf{Cas d'utilisation: XXX} \\
    \hline          
    \textbf{Description:}    
        \\ \textbf{Titre}: 
      \\ \textbf{But}: XXX
      \\ \textbf{Acteur}: XXX.
      \\\hline

      \textbf{Pré-conditions}:     \\\hline
      \textbf{Enchaînements nominaux}:
      \\ - XXX
      \\ - XXX
      \\ - XXX
      \\ - XXX
      \\ - XXX
      \\ - XXX
      \\ \hline \textbf{Exceptions}:
      \\ XXX
      \\ \hline \textbf{Post-conditions}:
      \\ XXX
      \\
    \hline      
\end{tabularx}
    \caption{Cas d'utilisation: XXX}

\end{table}

\end{document}

I tried many solution online but they don't seem to work, what I am getting is:

LaTeX Error: Can be used only in preamble. You have used a command in the main body of your document which should be used in the preamble. Make sure that \documentclass[…]{…} and all \usepackage{…} commands are written before \begin{document}.

How can I fix this?

  • First of all: You should not convert \textbf to obsolete \bf! Second: Package longtable does not provide X columns. You need a package like ltxtable or ltablex for longtables with X columns. Last but not least: Can you please add a minimal but working example (MWE) from \documentclass to \end{document} that shows what you've tried and can be used to reproduce the problem? – Schweinebacke Jun 6 '17 at 14:30
  • The given 'document' does not produce the error message, even after filling up with \usepackage{float} and \usepackage{tabularx} – user31729 Jun 6 '17 at 14:30
  • @Schweinebacke done. – Joker 00 Jun 6 '17 at 14:31
  • 1
    @Joker00: The updated document compiles for me and does not give the error message reported by you – user31729 Jun 6 '17 at 14:37
  • @ChristianHupfer it does compile but it's a table not a longtable. If i covert the table's declaration to longtable it gives the given error. – Joker 00 Jun 6 '17 at 14:42
1

The conversion of this table into a breakable table is very easy using package ltablex:

\documentclass[a4paper,12pt]{report}

\usepackage[T1]{fontenc}
\usepackage[francais]{babel}
\usepackage{lmodern} % Pour changer le pack de police.
\usepackage[utf8]{inputenc}% combination of utf8x and utf8 does not make sense

\usepackage{ltablex}
\keepXColumns % see ltablex manual

\begin{document}

\begin{tabularx}{\textwidth}{|X|}%{|X|}
    \caption{Cas d'utilisation: XXX}
    \label{CU1-label}
\endfirsthead
\hline          
    \textbf{Cas d'utilisation: XXX} \\
    \hline          
    \textbf{Description:}    
        \\ \textbf{Titre}: 
      \\ \textbf{But}: XXX
      \\ \textbf{Acteur}: XXX.
      \\\hline

      \textbf{Pré-conditions}:     \\\hline
      \textbf{Enchaînements nominaux}:
      \\ - XXX
      \\ - XXX
      \\ - XXX
      \\ - XXX
      \\ - XXX
      \\ - XXX
      \\ \hline \textbf{Exceptions}:
      \\ XXX
      \\ \hline \textbf{Post-conditions}:
      \\ XXX
      \\
    \hline      
\end{tabularx}

\end{document}

You could also use ltxtable. The solution would be almost equal:

\documentclass[a4paper,12pt]{report}

\usepackage[T1]{fontenc}
\usepackage[francais]{babel}
\usepackage{lmodern} % Pour changer le pack de police.
\usepackage[utf8]{inputenc}% combination of utf8x and utf8 does not make sense

\usepackage{ltxtable}
\usepackage{filecontents}

\begin{document}

\begin{filecontents}{\jobname-table.tmp}
\begin{longtable}{|X|}
    \caption{Cas d'utilisation: XXX}
    \label{CU1-label}
\endfirsthead
\hline          
    \textbf{Cas d'utilisation: XXX} \\
    \hline          
    \textbf{Description:}    
        \\ \textbf{Titre}: 
      \\ \textbf{But}: XXX
      \\ \textbf{Acteur}: XXX.
      \\\hline

      \textbf{Pré-conditions}:     \\\hline
      \textbf{Enchaînements nominaux}:
      \\ - XXX
      \\ - XXX
      \\ - XXX
      \\ - XXX
      \\ - XXX
      \\ - XXX
      \\ \hline \textbf{Exceptions}:
      \\ XXX
      \\ \hline \textbf{Post-conditions}:
      \\ XXX
      \\
    \hline      
\end{longtable}
\end{filecontents}
\LTXtable{\textwidth}{\jobname-table.tmp}

\end{document}

Note, I've removed all packages that are not needed to make your table to make the example more minimal.

BTW: This does not really look like a table. Maybe just using some boxes would be a better solution. Perhaps package tcolorbox could be useful too.

  • Thank you for your answer, I am marking it as a solving one. But I need to keep the caption in the bottom of the table, can this be done easily? – Joker 00 Jun 6 '17 at 14:47
  • @Joker00: Please read the longtable manual. You will the documentation of not only \endhead and \endfirsthead but also \endfoot and \endlastfoot. And note: We usually use table titles, to understand the meaning of a splitted table before we reach the end of the table. – Schweinebacke Jun 6 '17 at 14:51
  • Okay one last issue, actually the table is a part of a large report. When I try your solution in a separate document it works, but when I added it to my document it does not. I think it's an issue of pachages. How can I fix that? \\ PS: excuse my ignorance, I am new to Latex – Joker 00 Jun 6 '17 at 14:55
  • @Joker00: I don't have the second sight. I even do not know which solution you've tried. You've been told to make a MWE. And I cannot do anything else but tell you again: Add a MWE to your question or ask a new question with a MWE, if you want to use my answer in your example. – Schweinebacke Jun 6 '17 at 15:00
  • Should I edit the question with the MWE or create a new question? – Joker 00 Jun 6 '17 at 17:57

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.