2

I would like to make a definition in the form:

\def\nameMYdef#1{
if (length of text #1) <= (width of the text on the page (? \textwidth ?) ) 
then (center(#1))
else ( justification(#1))
}

Of course, this entry is only symbolic. I hope that it is understandable.

Best regards and I am looking forward to your reply.

Stefan Pinnow
  • 29,535
Maniek
  • 21

1 Answers1

2

Some definitional adjustments may be needed depending on how you want indents to be handled, and whether invocation is allowed NOT at the beginning of a paragraph.

\documentclass{article}
\usepackage{lipsum}
\def\nameMYdef#1{%
  \setbox0=\hbox{#1}\ifdim\wd0<\linewidth\relax\centerline{#1}\else #1\fi}
\begin{document}
\nameMYdef{Test of short line}

\nameMYdef{\lipsum[1]}
\end{document}
  • I don't know why, but it works! Thanks a lot !! – Maniek Apr 01 '20 at 15:15
  • @Maniek The logic is straightforward. It sets the content provided in \box0, and measures the width (\wd) of the box 0, comparing it to the allotted \linewidth. If it is less, use \centerline, otherwise, just set the multiline text as is. – Steven B. Segletes Apr 01 '20 at 15:21