0

How to align this vertical line at the end of the multi-column?

I searched, but I could not find an answer for such problem. This was originated from How to fix this Package array Error: Only one column-spec allowed?

enter image description here

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}

\usepackage[T1]{fontenc}
\usepackage[brazil]{babel}
\usepackage[a4paper, margin=2cm]{geometry}

\usepackage{newtxtext,newtxmath}
\usepackage{array,ragged2e,tabularx,multirow}

\begin{document}

\section{Custos}

    \begin{tabular}
    {|
        *1{@{\hspace{3.0pt}}>{ \RaggedRight\arraybackslash\hsize=1.1\hsize }p{3.9cm}|} % Item
        *1{@{\hspace{3.0pt}}>{ \RaggedRight\arraybackslash\hsize=1.1\hsize }p{1.9cm}|} % Quantidade
        *1{@{\hspace{3.0pt}}>{ \RaggedRight\arraybackslash\hsize=1.1\hsize }p{3.0cm}|} % Valor
        *1{@{\hspace{3.0pt}}>{ \RaggedRight\arraybackslash\hsize=1.1\hsize }p{2.6cm}|} % Valor
    }

        \hline Item &   Quantidade  &   Valor Unitário (R\$) & Valor Total (R\$) \\ \hline
        Total       & \multicolumn{2}{c|}{}                  & 165,00            \\ \hline

    \end{tabular}

\end{document}

Update 1

I find another question which seems to have the same problem, but it is not easy to understand what they did to fix it. I have to open my diff program to compare the original code and the fixed code:

  1. Why are the lines around my multicolumn cell misaligned?

enter image description here

On the right there is the fixed code, on the left is the original code.

That table goes from this:

enter image description here

Into this:

enter image description here


Update 2

By @David Carlisle comment, I find out that the question mentionated on the Update 1 is different from this one:

the edited question is an entirely different issue, please don't change the question that way invalidating all posted answers. The added question is the common issue of putting | on the wrong side of the cell boundary, the original question is unrelkated to that and just about your mis-use of

9
  • why are you replacing the default tabcolsep of 4pt by a space of 3pt, just on one side of each vertical rule? Apr 27, 2017 at 18:52
  • Nothing special, just to keep the code explicit, rather than implicit. So I can configure the table more easily. I would do this just for now while I am not experienced with latex tables.
    – user
    Apr 27, 2017 at 18:54
  • But why do you explicitly want 4pt on one side of each rule and 3pt on the other??? Apr 27, 2017 at 18:55
  • Also \hsize=1.1\hsize is just wrong. Apr 27, 2017 at 18:56
  • I copied it from my other table, which I had to remove a little the adjust to stop a overfull warning. About \hsize I do not know what is it doing exactly. This is the original table: tex.stackexchange.com/questions/366155/…, on the answer there, the guy put \hsize on it, but looks like he did not say why.
    – user
    Apr 27, 2017 at 19:08

4 Answers 4

3

Your preamble has obscured the necessary column spec:

enter image description here

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}

\usepackage[T1]{fontenc}
\usepackage[brazil]{babel}
\usepackage[a4paper, margin=2cm]{geometry}

\usepackage{newtxtext,newtxmath}
\usepackage{array,ragged2e,tabularx,multirow}

\begin{document}

\section{Custos}

    \begin{tabular}
    {|
        *1{@{\hspace{3.0pt}}>{ \RaggedRight\arraybackslash\hsize=1.1\hsize }p{3.9cm}|} % Item
        *1{@{\hspace{3.0pt}}>{ \RaggedRight\arraybackslash\hsize=1.1\hsize }p{1.9cm}|} % Quantidade
        *1{@{\hspace{3.0pt}}>{ \RaggedRight\arraybackslash\hsize=1.1\hsize }p{3.0cm}|} % Valor
        *1{@{\hspace{3.0pt}}>{ \RaggedRight\arraybackslash\hsize=1.1\hsize }p{2.6cm}|} % Valor
    }

        \hline Item &   Quantidade  &   Valor Unitário (R\$) & Valor Total (R\$) \\ \hline
        Total       & \multicolumn{2}{c|@{\hspace{3.0pt}}}{}                  & 165,00            \\ \hline

    \end{tabular}

\end{document}

I left in the \hsize=1.1\hsize but it is completely wrong! Also as commented under the question, the @ usage here is making the vertical rules no longer centred in the inter-column space.

2
  • Thanks, I understand the problem was the spacing adjusting I was doing on each column from the main table. Therefore I also needed to adjust the column on the inner column table. To fix it was need to change \multicolumn{2}{c|}{} to \multicolumn{2}{c|@{\hspace{3.0pt}}}{}.
    – user
    Apr 27, 2017 at 19:23
  • @user that lines up the | as I show in my answer but it is still very wrong, the | should be centred in the space between the columns and you are making it off centre using @ in this way. Apr 27, 2017 at 19:25
2

You are over-complicating things here. If you need to adjust the tabcolsep, just overwrite the default 4pt by \setlength{\tabcolsep}{3pt}.

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}

\usepackage[T1]{fontenc}
\usepackage[brazil]{babel}
\usepackage[a4paper, margin=2cm]{geometry}

\usepackage{newtxtext,newtxmath}
\usepackage{array,ragged2e,tabularx,multirow}

\newcolumntype{R}[1]{>{\RaggedRight}p{#1}}
\setlength{\tabcolsep}{3pt}
\begin{document}

\section{Custos}

\begin{tabular}
    {| R{3.9cm}| % Item
       R{1.9cm}| % Quantidade
       R{3.0cm}| % Valor
       R{2.6cm}| % Valor
    }
    \hline 
    Item    & Quantidade    &   Valor Unitário (R\$) & Valor Total (R\$) \\ \hline
    Total   & \multicolumn{2}{c|}{}                  & 165,00            \\ \hline
\end{tabular}

\end{document}
2
  • \RaggedRight do not need the \arraybackslash
    – user2478
    Apr 27, 2017 at 19:08
  • @Herbert: True, thanks, I tend to use it a lot.
    – AboAmmar
    Apr 27, 2017 at 19:19
1

A solution for the first question:

The culprit is the @{\hspace{3pt}}, which you didn't used in \multicolumn. But it's better to set \tabcolsep to 3pt for the entire table:

\documentclass[12pt, brazil]{article}
\usepackage[utf8]{inputenc}

\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage[a4paper, margin=2cm, showframe]{geometry}

\usepackage{newtxtext,newtxmath}
\usepackage{array,ragged2e,tabularx,multirow}

\begin{document}

\section{Custos}

{\setlength\tabcolsep{3pt}
    \begin{tabular}
    {|>{\RaggedRight\arraybackslash}p{3.9cm}|% Item
    >{\RaggedRight\arraybackslash}p{1.9cm}| % Quantidade
    >{\RaggedRight\arraybackslash}p{3.0cm}| % Valor
    >{\RaggedRight\arraybackslash}p{2.6cm}|} % Valor
        \hline %
        Item & Quantidade & Valor Unitário (R\$) & Valor Total (R\$) \\ %
        \hline
        Total & \multicolumn{2}{c|}{Lalala} & 165,00 \\ %
        \hline
    \end{tabular}}

\end{document} 

enter image description here

1

enter image description here

I add to geometry option showframe for showing page layout. Now is better visible table size and how unnecessary complicated is your column style definitions.

MWE:

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}

\usepackage[T1]{fontenc}
\usepackage[brazil]{babel}
\usepackage[showframe,
            a4paper, margin=2cm]{geometry}

\usepackage{newtxtext,newtxmath}
\usepackage{array,ragged2e,tabularx,multirow}

\newcolumntype{R}[1]{>{\RaggedRight}p{#1}}
%\setlength{\tabcolsep}{3pt}
\begin{document}

\section{Custos}

\begin{tabular}
    {| R{4cm}| % Item
       R{2cm}| % Quantidade
       R{3.5cm}| % Valor
       R{3cm}| % Valor
    }
    \hline
    Item    & Quantidade    &   Valor Unitário (R\$) & Valor Total (R\$) \\ \hline
    Total   & \multicolumn{2}{R{4.9cm}|}{}                  & 165,00            \\ \hline
\end{tabular}    
\end{document}

Edit: Maybe you will like the following solution:

enter image description here

where for table is used tabularx environment:

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}

\usepackage[T1]{fontenc}
\usepackage[brazil]{babel}
\usepackage[showframe,
            a4paper, margin=2cm]{geometry}

\usepackage{newtxtext,newtxmath}
\usepackage{array,ragged2e,tabularx,multirow}
\newcolumntype{R}{>{\RaggedRight}X|}

\begin{document}

\section{Custos}

    \begin{tabularx}{\linewidth}
    {| >{\hsize=1.75\hsize}R   % Item
                           R   % Quantidade
       >{\hsize=1.25\hsize}R   % Valor
                           R   % Valor
    }
    \hline
Item    &   Quantidade            &   Valor Unitário (R\$)  & Valor Total (R\$) \\ \hline
Total   &   \multicolumn{2}{R}{}                            &   165,00            \\ \hline
    \end{tabularx}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .