2

I was making frontmatter for my thesis.

The title page contains something like:

Title:            some centered text
Instructors:           somebody
...:                     ...

So I placed them in a two-column tabular, with the first column left aligned and the second centered. But when the second column has a long text (long title or many instructors), it turns into:

Title:       some centered very very very
                    looooong text
Instructors:           somebody
...:                     ...

While I want it to be left aligned instead, something like this:

Title:       some centered very very very
             looooong text
Instructors:           somebody
...:                     ...
  • \multicolumn{1}{c}{...} when the default is p{<width>}, or vice verses. Or did you put "loooooong text" in a separate row? – John Kormylo Nov 10 '18 at 20:34
  • @JohnKormylo No. I just want it to be done automatically. One does not need to pay attention to the length of the content. – jiewuza Nov 11 '18 at 1:07
  • You can have automatic line breaking only if you specify a width. If you don't want to specify a width, use \multicolumn{1}{l}{...} for every line of text. – John Kormylo Nov 11 '18 at 15:24
  • @JohnKormylo Yes, I use a p column with width. The problem here is that I want the text centered if no more than one line, otherwise left aligned. – jiewuza Nov 13 '18 at 4:32
  • Ah, you want a single-line check. That can be done. I suppose you want me to create your MWE for you. (sigh!). – John Kormylo Nov 13 '18 at 14:28
1

Here is a proof of concept.

\documentclass{article}

\newcommand{\SLcheck}[2]% #1 = width, #2 = text
{\bgroup
  \sbox0{#2}%
  \ifdim\wd0>#1\relax \parbox[t]{#1}{#2}%
  \else \usebox0
  \fi
 \egroup}

\begin{document}
\begin{tabular}{lc}
Title: & \SLcheck{4cm}{some justified very very very looooong text} \\
Instructors: & \SLcheck{4cm}{somebody}
\end{tabular}
\end{document}

The next step is to create a new column type using the array package.

\documentclass{article}
\usepackage{array}

\newenvironment{SLcheck}[1]% #1 = width
{\def\myarg{#1}% local definition
  \begin{lrbox}{0}}{\end{lrbox}%
  \ifdim\wd0>\myarg\relax \parbox[t]{\myarg}{\unhbox0}%
  \else \box0
  \fi
}

\newcolumntype{Q}[1]{>{\SLcheck{#1}}c<{\endSLcheck}}

\begin{document}
\begin{tabular}{lQ{4cm}}
Title: & some justified very very very looooong text \\
Instructors: & somebody
\end{tabular}
\end{document}
  • Nice! learned a lot from your demo. – jiewuza Nov 14 '18 at 14:16

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.