2

Is there an option to define custom alignment for text? For example, to make your own \begin{flushright}...\end{flushright}?

In particular, I want to have an alignment (for text) that it is centred when there is only one line, but is left-aligned when it needs to have more than one line (i.e. when there is a linebreak). Of course, this can be done manually, but this doesn't sound very convenient.

5
  • 2
    Caption uses a single line;check. It puts the text into an \hbox and measures the width. If too wide, it uses paragraph formatting. May 19 '20 at 1:07
  • 1
    @JohnKormylo I don't understand what you mean. What is "caption"?
    – Jim
    May 19 '20 at 6:27
  • \caption is used by floats (figure & table) to 1) increment the figure number, 2) label the float and 3) add an entry into the list of figures. It is part of standard LaTeX, but a much fancier version is available in the caption package. May 19 '20 at 16:05
  • Do you know if it's possible to have the same effect as \caption with singlelinecheck, but for, say, a minipage instead?
    – Jim
    May 20 '20 at 3:23
  • 1
    Of course it is possible. It would look like wipet's solution, only using an \hbox and \ifdim\wd0>\columnwidth. See also tex.stackexchange.com/questions/233687/… May 20 '20 at 3:37
3

For example, you can define the macro \centeredpar as follows:

\def\centeredpar #1\par{\noindent
   \setbox0 =\vbox{\noindent #1\par
   \expandafter}\ifnum\prevgraf=1 \hfil \else \indent \fi 
   #1\par 
}

% test

\centeredpar Short text.

\centeredpar Long text. Sgsh jk k kk fr j ewh ej wwu ye ew gh
             hq hhad hj adj, had j dahj.
             Sgsh jk k k tuiu iwe huwi tyac  hq hhad hj adj, had j dahj.

% text of \centeredpar must be finished by empty line.

The paragraph is typeset into working \vbox and the number of paragraph lines i measured by \prevgraf TeX primitive. The \hfil primitive adds a glue before the real paragraph. The end of paragraph is processed by internal TeX algorithm, with \parfillskip register which has the same "force" as \hfil by default. So, the text is centered. Else \indent is added and multiline pararagraph is printed. You can omit \indent in this macro if you need not to start with the \indent.

6
  • Is it possible to make such an environment and make it work with math?
    – antshar
    May 19 '20 at 9:33
  • @antshar Please, be more clear what is your intent. Maybe, you can open new thread. If you mean that TeX will automatically break long display formula the I mean that this is not so simple and it is against the intent of TeX: user have to decide where break points are in math formula because automaton is not able to understand the formula well.
    – wipet
    May 19 '20 at 12:06
  • I cannot say I understand this completely, but I like this solution. I think \else \indent can be entirely omitted and there will no problem, right? Also, \vbox{...} is there just to count the lines? Because it seems that it isn't printed in the .pdf. Finally, can you explain at which point \ifnum stops expanding to let \expandafter bring back the } token?
    – Jim
    May 20 '20 at 1:11
  • Another question: It seems that \centeredpar gives an error if I try to use it inside a custom environment, for example \newenvironment{textcentered}{\centeredpar}{}
    – Jim
    May 20 '20 at 2:34
  • @Jim 1) \else\indent can be omited. 2. \setbox0=\vbox{...} means that the typesetting material is saved to box 0, no printed. The box 0 is not used later, may be other macro use \setbox0=, then the contents is over-writen (this does no matter). The \vbox closes group when it finishes, so I meant that the \expandafter is needed. But more true is that it is not needed because \prevgraph is special primitive register with global value only.
    – wipet
    May 20 '20 at 11:52
0

Inspired by the comments and answers here I made this code which uses the environ package:

\usepackage{environ}

\environbodyname\yourinput

\NewEnviron{autocentredtext}{%
    \setbox0=\hbox{\yourinput}
    \ifdim\wd0<\textwidth
    \begin{center}
    \yourinput
    \end{center}
    \fi
    \noindent\yourinput
}%[\ignorespacesafterend]

This creates an environment which is implemented by \begin{autocentredtext}...\end{autocentredtext} which first reads your text input, checks if it's shorter than \textwidth, and if it is, it gets centred or otherwise it becomes non-indented.

One might want to add [\ignorespacesafterend] at the end of the above code to fix some possible spacing issues.


Unfortunately, it seems that this cannot be used with \newenvironment. For example,

\newenvironment{autocentering}{\begin{autocentredtext}}{\end{autocentredtext}}

creates an error. If one uses \NewEnviron instead of \newenvironment, they should be able to overcome this.

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.