2

Is there a way to align tables in LaTeX?

I was writing a document with several tables in the same page, and often the edges don't match.

I would like to have tables aligned in both left and right edge.

\paragraph{Articoli}
\begin{large}
\begin{tabular}{ | l | l | l | p{3cm} |}
\hline
\textbf{Attributo} & \textbf{Tipo} & \textbf{Byte} & \textbf{Complessivo} 
\\    \hline
& & & 1500 occorrenze \\ \hline
Codice & Char(7) & 7 & 10,7 kB \\ \hline
Nome  &  Varchar (100) & 100 & 150 kB  \\ \hline
Marca & Varchar (100) & 100 & 150 kB \\ \hline
Prezzo & Money & 8 & 12,2 kB \\ \hline
Disponibile & Integer & 4 & 6.2 kB \\ \hline
Soglia & Integer & 4 & 6.2 kB \\ \hline
Reparto & Char (4) & 4 & 6.2 kB \\ \hline
Totale &  & &   321 kB      \\ \hline
Dati + Block Header & & & 322 kB \\ \hline
    \end{tabular}
\paragraph{Reparti}
 \begin{tabular}{ | l | l | l | p{3cm} |}
    \hline
    \textbf{Attributo} & \textbf{Tipo} & \textbf{Byte} & \textbf{Complessivo}        
     \\ \hline
    & & & 120 occorrenze \\ \hline
    Codice & Char(4) & 4 & 0,5 kB  \\ \hline
    Nome  &  Varchar (100) &  100 & 12.1 kB \\ \hline
    Sezione & Char(4) & 4 & 0.5 kB \\ \hline
    Totale &  & &  12.2 kB        \\ \hline
    Dati + Block Header & & & 12.3 kB \\ \hline
        \end{tabular}
  \paragraph{Pagamenti}
 \begin{tabular}{ | l | l | l | p{3cm} |}
    \hline
    \textbf{Attributo} & \textbf{Tipo} & \textbf{Byte} & \textbf{Complessivo} \\ \hline
    & & & 10 occorrenze  \\ \hline
    Codice & Char(4) & 4 & 0,1 kB \\ \hline
    Nome  &  Varchar (100) &  100 & 1 kB  \\ \hline
            Totale &  & &  1.1 kB       \\ \hline
            Dati + Block Header & & & 1.2 kB  \\ \hline
        \end{tabular}
  • 3
    you can use fixed width columns p{2cm} rather than l or do other things, it always helps if you provide a complete small document that shows the problem and allows people to test answers. – David Carlisle Dec 30 '14 at 12:02
  • 1
    Who downvoted this? ... Give the OP some time to provide a MWE – user31729 Dec 30 '14 at 12:15
  • Added code, and improved formatting. – EagleOne Dec 30 '14 at 12:17
  • Tried to remove the paragraph and use \textbf instead but the right edges don't match (the left ones do). So do i have to remove \paragraph or could i use them in another way? And if i have to remove them, how could I align the tables on the right edge? – EagleOne Dec 30 '14 at 12:36
  • They didn't even really line up on the right edge in the posted document (overfull boxes by 46.0867 and 46.01724 pt, respectively, on a US letter page with default margins). It's just coincidence that a large "Reparti" is about the same length as a regular size "Articoli", and you probably didn't want one label's font size to be larger than the other anyway. – Mike Renfro Dec 30 '14 at 13:26
7

I think you're abusing the \paragraph macro. I believe it's preferable to set up a new macro that typesets its argument ("articoli", "reparti", and "pagamenti", resp.) in bold in a "box" of a given width.

A separate comment: In the right-hand column of the tables, you seem to be using commas and periods ("full stops") to denote the decimal marker. I believe it's good practice to standardize the notation.

enter image description here

\documentclass{article}
\newlength\mylen
\settowidth{\mylen}{\bfseries Pagamenti \ \ }
\begin{document}
\noindent\parbox{\mylen}{\bfseries Articoli}
\begin{tabular}{ | l | l | l | p{3cm} |}
\hline
\textbf{Attributo} & \textbf{Tipo} & \textbf{Byte} & \textbf{Complessivo} 
\\    \hline
& & & 1500 occorrenze \\ \hline
Codice & Char(7) & 7 & 10,7 kB \\ \hline
Nome  &  Varchar (100) & 100 & 150 kB  \\ \hline
Marca & Varchar (100) & 100 & 150 kB \\ \hline
Prezzo & Money & 8 & 12,2 kB \\ \hline
Disponibile & Integer & 4 & 6.2 kB \\ \hline
Soglia & Integer & 4 & 6.2 kB \\ \hline
Reparto & Char (4) & 4 & 6.2 kB \\ \hline
Totale &  & &   321 kB      \\ \hline
Dati + Block Header & & & 322 kB \\ \hline
    \end{tabular}


\bigskip\noindent\parbox{\mylen}{\bfseries Reparti}
 \begin{tabular}{ | l | l | l | p{3cm} |}
    \hline
    \textbf{Attributo} & \textbf{Tipo} & \textbf{Byte} & \textbf{Complessivo}        
     \\ \hline
    & & & 120 occorrenze \\ \hline
    Codice & Char(4) & 4 & 0,5 kB  \\ \hline
    Nome  &  Varchar (100) &  100 & 12.1 kB \\ \hline
    Sezione & Char(4) & 4 & 0.5 kB \\ \hline
    Totale &  & &  12.2 kB        \\ \hline
    Dati + Block Header & & & 12.3 kB \\ \hline
        \end{tabular}

\bigskip\noindent\parbox{\mylen}{\bfseries Pagamenti}
 \begin{tabular}{ | l | l | l | p{3cm} |}
    \hline
    \textbf{Attributo} & \textbf{Tipo} & \textbf{Byte} & \textbf{Complessivo} \\ \hline
    & & & 10 occorrenze  \\ \hline
    Codice & Char(4) & 4 & 0,1 kB \\ \hline
    Nome  &  Varchar (100) &  100 & 1 kB  \\ \hline
            Totale &  & &  1.1 kB       \\ \hline
            Dati + Block Header & & & 1.2 kB  \\ \hline
        \end{tabular}
\end{document}
| improve this answer | |
  • Really thanks, it helped a lot and yes, the notation standardization is a very good practice – EagleOne Dec 30 '14 at 14:40

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.