2

Want to use the empty space between X and r:

\begin{tabularx}{\textwidth}{Xrrrr}
  \hline
 & X1 & X2 & X3 & X4 \\ 
  \hline
  A & 1\% & 2\% & 2\% & 2.0\% \\ 
  B & 2\% & 3.0\% & 4.0\% & 30.5\% \\ 
  \hline
\end{tabularx}

I do not use XXXXX because flush right is necessary.

4

I can see two solutions:

  • Keep using the tabularx environment, but define a flushright version of the X column type and use it for columns 2 to 5 (instead of r); and

  • switch to a tabular* environment, keep using r for columns 2 to 5, and use an l column type for the first column -- or maybe a p column if linebreaks are needed in the first column.

enter image description here

The tabularx environment works by expanding the widths of the columns (of type X and R), whereas the tabular* environment works by expanding the amount of inter-column white-space. In the screenshot above, the five columns in the middle table have equal widths (though it's not easy to tell so immediately because the first column's material is located at the far left and the second column's material is at the far right); in the third table, which uses a tabular* environment, the four inter-column spaces are made equally wide.

For the simple table at hand, the tabular*-based solution seems preferable. However, if the table is more complicated, the tabularx-based solution could work better for you. (After all, I would assume that the first column will eventually contain more than just the letters A and B...)

\documentclass[]{article}
\usepackage{tabularx}
% Define a flushright version of "X" column type
\newcolumntype{R}{>{\raggedleft\arraybackslash}X}

\begin{document}
\noindent
initial version

\smallskip\noindent
\begin{tabularx}{\textwidth}{Xrrrr}
  \hline
 & X1 & X2 & X3 & X4 \\
  \hline
  A & 1\% & 2\% & 2\% & 2.0\% \\
  B & 2\% & 3.0\% & 4.0\% & 30.5\% \\
  \hline
\end{tabularx}

\bigskip\noindent
\verb|tabularx| environment and columns of type \texttt{X} and \texttt{R}

\smallskip\noindent 
\begin{tabularx}{\textwidth}{XRRRR}
  \hline
 & X1 & X2 & X3 & X4 \\
  \hline
  A & 1\% & 2\% & 2\% & 2.0\% \\
  B & 2\% & 3.0\% & 4.0\% & 30.5\% \\
  \hline
\end{tabularx}


\bigskip\noindent
\verb|tabular*| environment and columns of type \texttt{l} and \texttt{r}

\smallskip\noindent
\begin{tabular*}{\textwidth}{l@{\extracolsep{\fill}}rrrr}
  \hline
 & X1 & X2 & X3 & X4 \\
  \hline
  A & 1\% & 2\% & 2\% & 2.0\% \\
  B & 2\% & 3.0\% & 4.0\% & 30.5\% \\
  \hline
\end{tabular*}

\end{document}
  • XRRRR is not accepted: Overfull \hbox (2.21997pt too wide) in paragraph at lines 30.... I do not need tabular* because I dont want to use it but now I will try how it looks. tabularx gives a 100% wide table with the right flushed lines in between rows and fullfills therefore the purpose – b4154 Dec 8 '15 at 8:12
  • Im using R Sweave maybe therefore for tabularx XRRRR is not accepted – b4154 Dec 8 '15 at 8:17
  • Using {l@{\extracolsep{\fill}} for tabularx also works fine as with tabular* – b4154 Dec 8 '15 at 8:18
  • @b4154 - For the R column type to work, it has to be defined as in my example solution. – Mico Dec 8 '15 at 9:16
0

When using tabularx you can set the width of the table, in this case \textwidth, which the X column type uses up.

If you want it to be shorter, other than changing the other columns or using another tabular, you can redefine that length, like .5\textwidth to be half of the page width, and so on.

example image

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.