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?

David Carlisle
  • 757,742
AbdelKh
  • 258

1 Answers1

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.

Moriambar
  • 11,466
Schweinebacke
  • 26,336
  • 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? – AbdelKh Jun 06 '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 06 '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 – AbdelKh Jun 06 '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 06 '17 at 15:00
  • Should I edit the question with the MWE or create a new question? – AbdelKh Jun 06 '17 at 17:57
  • @Joker00 As I wrote: If you want to ask why my answer does not work with your new example, write a question referencing this one. If you only want to add your own example you can add it to your question. Generally the edit of a question should avoid using code from already existing answers making the answer useless. In such a case I would have to delete my answer. BTW: To avoid such problems, you should always add a MWE to your question to make them clear. – Schweinebacke Jun 06 '17 at 18:08