3

enter image description here

For my study I need to put some information in a tabular. I'm using tabularx and to determine to width of my columns I use \parbox. That all works very good, but the text on both columns doesn't start at the same level. I'd like to have the text on the left column at the same hight as the text on the right column, how can I do that ?

\documentclass{article}     
\usepackage{polyglossia}    
\setmainlanguage{french}    
\setotherlanguages{english,german,latin,italian,spanish,russian,greek}     
\usepackage{tabularx}    

\begin{document}    
\begin{center}    
    \begin{tabularx}{\linewidth}{|@{}>{\bfseries}l|@{\hspace{.5em}}X@{}|}
   \hline  \parbox{3cm}{text} & text \\     
 \hline \parbox{3cm}{text}  & text  \\    
  ...        
\end{tabularx}    
\end{center}    
\end{document}    
  • 2
    The middle of the left cell is aligned to the base line of the first line in the second cell. This can be fixed by option t : \parbox[t]{3cm}{...}. An easier way is using p{3cm} without an additional \parbox for the first column. – Heiko Oberdiek Aug 8 '15 at 19:48
  • 1
    don't use parbox in tables – David Carlisle Aug 8 '15 at 20:18
5

For the first column, don't specify l as the column type and then use a \parbox (of width 3cm) to typeset its material. Instead, use p{3cm} directly as the column type specifier.

In view of the rather narrow measure that's set for the first column, I would use ragged-right rather than full (ie., left and right) justification.

enter image description here

\documentclass{article}     
\usepackage{polyglossia}    
\setmainlanguage{french}    
\setotherlanguages{english,german,latin,italian,spanish,russian,greek}     
\usepackage{tabularx} 
\usepackage{ragged2e} % for "\RaggedRight" macro  
\newcolumntype{P}[1]{>{\RaggedRight\arraybackslash}p{#1}}
\usepackage{lipsum} % for filler text

\begin{document}    
\noindent  
\begin{tabularx}{\linewidth}{ @{}
    >{\bfseries}P{3cm} | @{\hspace{.5em}}
    X @{}}
\hline  
Once upon a time, there was an enchanted castle \dots & 
\lipsum*[1] \\     
\hline 
text  & text  \\           
\end{tabularx}       
\end{document} 

Addendum to address @u2berggeist's follow-up question:

can you explain the necessity of the \arraybackslash command in the column type definition

Let me begin with an excerpt from the user guide of the array package:

\arraybackslash: Restore \\ for use in array and tabular environment (after \raggedright etc.).

This is admittedly rather terse. Let me attempt to provide a somewhat wordier explanation of the purpose of \arraybackslash.

  • When using \newcolumntype to define a new column type, the meaning of \\ gets changed (for reasons I admit to never having fully grasped). The upshot, though, is that \\ is no longer the macro that creates a line break; one has to use \tabularnewline instead. The instruction \arraybackslash simply restores the widely-expected behavior of \\. (I don't think I've every met anyone who prefers to type \tabularnewline over \\...)

  • It is the case, in the code shown above, that there are no forced line breaks in the P column (the first column). Hence, it was not strictly necessary to employ \arraybackslash in the \newcolumntype definition of P. So why did I use \arraybackslash anyway? I did it for two reasons.

  • First, it never hurts to do so -- unless one has a hankering for typing \tabularnewline...

  • Second, if the P column were used in the final column instead of in the first column, the failure to have employed \arraybackslash in the \newcolumntype instruction would generate some rather cryptic error messages which would certainly baffle everybody not named David Carlisle. The postings on this site often turn up in Google searches, even years after they were made. This answer, say, might turn up in a search for "latex table raggedright column". A casual reader of this answer might gladly note

    \newcolumntype{P}[1]{>{\RaggedRight\arraybackslash}p{#1}}
    

    and might even take note of the fact that it's necessary to load the ragged2e package (as it defines \RaggedRight). What a casual reader may well miss -- and, in fact, should be excused for having missed! -- is that in the tabularx environment at hand, the P column type doesn't get used in the final column. Had I posted

    \newcolumntype{P}[1]{>{\RaggedRight}p{#1}}
    

    the answer would have still "worked" for the given tabularx environment; however, it would have needlessly risked creating huge amounts of frustration among future users who performed a web search with the terms "latex table raggedright column" and were interested in using the P column type for the final, i.e., right-hand-most, column of their tabular-like environment.

Did I succeed in answering your question?

  • 1
    Old post, but can you explain the necessity of the \arraybackslash command in the column type definition? This was the missing piece was to a problem I've been working on, but I have no idea what it does. – u2berggeist Jul 16 at 2:28
  • 1
    @u2berggeist - Please see the addendum I posted to address your follow-up question. – Mico Jul 16 at 4:44
  • 1
    You managed to both answer my question as well as address my exact situation: searching for "latex table raggedright column" (or "tabularx raggedright column" in my case as I needed the X column type), applying the solution on the last column of the table, recognizing the need to load ragged2e (though I didn't end up doing it), and getting cryptic error messages without the \arraybackslash. I'm guessing the mentioning of the last column is due to the fact that the `\` get's used in the last column? – u2berggeist Jul 16 at 11:28
  • 1
    @u2berggeist - Wow, I had no idea I was that close with my guess about the search term! Thanks for letting me know. :-) I'm also glad that you were able to figure out on your own that an \arraybackslash directive would restore order. :-) – Mico Jul 16 at 11:50
  • 1
    Yeah, I was using \\ , not \tabularnewline. Honestly, that's the first time I've seen that one. And I was just playing a game of "find the differences" between my code and yours. Nothing too special there. ;) – u2berggeist Jul 16 at 14:39

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.