1

I'm new at LaTeX and I'm trying to Excel2Latex my table.

This is my latex document:

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{multirow}
\usepackage{float} 
\usepackage{tabularx}

\begin{document}


    \begin{table}[htbp]
        \centering
        \caption{Add caption}
        \begin{tabularx}{\textwidth}{lrrr}
            \hline
            Use Case \# & \multicolumn{1}{l}{Name} &       &  \\ \hline
            Goal in Contect & \multicolumn{1}{l}{Obiettivo} &       &  \\ \hline
            Preconditions & \multicolumn{1}{l}{ok} &       &  \\ \hline
            Success End Conditions & \multicolumn{1}{l}{ok} &       &  \\ \hline
            Failed End Conditions & \multicolumn{1}{l}{ok} &       &  \\ \hline
            Primary Actor & \multicolumn{1}{l}{ok} &       &  \\ \hline
            Trigger & \multicolumn{1}{l}{ok} &       &  \\ \hline
            Descriptions & \multicolumn{1}{l}{Step} & \multicolumn{1}{l}{User Acton} & \multicolumn{1}{l}{System} \\ \hline
            & \multicolumn{1}{l}{\#1} & \multicolumn{1}{p{25.355em}}{Lorem ipsum dolor sit amet, consectetur adipiscing elit.\newline{}Mauris posuere nisl et risus viverra sagittis ut et est. Morbi nec nunc imperdiet, pulvinar quam sed, lobortis arcu. Nulla sagittis tortor ac eros tincidunt lacinia. Cras ut venenatis ante, eu condimentum arcu. Ut luctus rutrum fermentum. Mauris aliquam magna ex, vel.} &  \\ \hline
            Extensions & \multicolumn{1}{l}{Step} & \multicolumn{1}{l}{User Action} & \multicolumn{1}{l}{System} \\ \hline
            &       &       &  \\ \hline
            Subvariations & \multicolumn{1}{l}{Step} & \multicolumn{1}{l}{User Action} & \multicolumn{1}{l}{System} \\ \hline
            &       &       &  \\ \hline
            Notes &       &       &  \\ \hline
        \end{tabularx}%
        \label{tab:addlabel}%
    \end{table}%

\end{document}

but unfortunately this goes out of the right margin of the page, as you can see here

  • How can I fix?
0
2
  • if you use tabularx at least one column type should be of X type (or its derivative)
  • \multicolumn{1}{p{25.355em}{ ... long text ...} is to wide. Remove this command and use for this column X column type
  • Remove all \mullticolumn{1}{l}{...}. They are just a clutter. Rather set column types accordingly (from r to l)
  • I would remove the most of horizontal lines
  • for main ones I would use rules defined in the booktabs package
\documentclass[a4paper]{article}
\usepackage{ragged2e}
\usepackage{booktabs, makecell, tabularx}
\newcolumntype{L}{>{\RaggedRight\hspace{0pt}}X}

%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
    \begin{table}[htbp]
\caption{Add caption}
\label{tab:addlabel}
    \setcellgapes{2pt}
    \makegapedcells
\begin{tabularx}{\textwidth}{@{} l l L r @{}}
    \toprule
Use Case \#     & Name      &       &           \\ 
    \midrule
Goal in Contect & Obiettivo &       &           \\ 
%    \hline
Preconditions   & ok        &       &           \\ 
%    \hline
Success End Conditions 
                & ok        &       &           \\ 
%    \hline
Failed End Conditions 
                & ok        &       &           \\ 
%    \hline
Primary Actor   & ok        &       &           \\ 
%   \hline
Trigger         & ok        &       &           \\
    \hline
Descriptions    & Step      & User Action    
                                    & System    \\ 
%    \hline
                & \#1       & Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris posuere nisl et risus viverra sagittis ut et est. Morbi nec nunc imperdiet, pulvinar quam sed, lobortis arcu. Nulla sagittis tortor ac eros tincidunt lacinia. Cras ut venenatis ante, eu condimentum arcu. Ut luctus rutrum fermentum. Mauris aliquam magna ex, vel.
                                    &           \\
    \hline
Extensions      & Step      & User Action
                                    & System    \\ 
%    \hline
                &           &       &           \\ 
    \hline
Subvariations   & Step      & User Action
                                    & System    \\ 
                &           &       &           \\ 
    \hline
Notes           &           &       &           \\ 
    \bottomrule
\end{tabularx}%
    \end{table}%
\end{document}

enter image description here

(red lines indicate text borders)

Addendum: Considering @Barbara Beeton comment, above MWE can be redesign as follows:

\documentclass[a4paper]{article}
\usepackage{ragged2e}
\usepackage{booktabs, makecell, multirow, tabularx}
\newcolumntype{L}{>{\RaggedRight\hspace{0pt}}X}
\newlength{\pwidth}

\begin{document}
    \begin{table}[htbp]
\caption{Add caption}
\label{tab:addlabel}
    \settowidth{\pwidth}{Preconditions}
\begin{tabularx}{\textwidth}{@{} >{\raggedright}m{\pwidth} % <--- changed
                                 l L r @{}}
    \toprule
Use Case \#     & Name      &       &           \\
    \midrule
Goal in Contect & Obiettivo &       &           \\
    \addlinespace    % <--- added, instead of \hline
Preconditions   & ok        &       &           \\
    \addlinespace
Success End Conditions
                & ok        &       &           \\
    \addlinespace
Failed End Conditions
                & ok        &       &           \\
    \addlinespace
Primary Actor   & ok        &       &           \\
   \addlinespace
Trigger         & ok        &       &           \\
    \midrule
Descriptions    & Step      & User Action
                                    & System    \\
    \addlinespace
                & \#1       & Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris posuere nisl et risus viverra sagittis ut et est. Morbi nec nunc imperdiet, pulvinar quam sed, lobortis arcu. Nulla sagittis tortor ac eros tincidunt lacinia. Cras ut venenatis ante, eu condimentum arcu. Ut luctus rutrum fermentum. Mauris aliquam magna ex, vel.
                                    &           \\
    \midrule
Extensions      & Step      & User Action
                                    & System    \\
    \addlinespace
                &           &       &           \\
    \midrule
Subvariations   & Step      & User Action
                                    & System    \\
                &           &       &           \\
    \midrule
Notes           &           &       &           \\
    \bottomrule
\end{tabularx}
    \end{table}
\end{document}

enter image description here

4
  • +1 for getting rid of all \hline directives.
    – Mico
    Mar 18 '20 at 15:15
  • 1
    @Mico, Thank you!
    – Zarko
    Mar 18 '20 at 15:16
  • I'd go a little further and make the leftmost column a paragraph, ragged right, with just enough width to accommodate the longest initial word ("Preconditions" or "Subvariations"). Leave the text-heavy column as the variable one; narrowing another column will allow that one to need fewer lines. Mar 18 '20 at 17:31
  • @barbarabeeton, thank you very much for suggestion. I consider it in addendum to original answer.
    – Zarko
    Mar 18 '20 at 17:51

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.