4

I am trying align text such that the first line is fully left aligned, and the second line is centered. The following is the desired behaviour with the associated code.

\begin{flushleft}
Step 2:
\end{flushleft}
\begin{center}
chi-squared-test (assuming data is normal)
\end{center}

correct result

I usually use \raggedright and \centering like the following, but this gives me undesirable behaviour.

\raggedright{Step 2:}
\centering{chi-squared-test (assuming data is normal)}

incorrect result

I also tried inserting a \newline after the first line, but this also gives me undesirable behaviour.

\raggedright{Step 2:}
\newline
\centering{chi-squared-test (assuming data is normal)}

incorrect result

I have the working section in the first code block, but in an effort to write less, I would prefer to use \raggedright and \centering instead. What am I doing incorrectly? Why is the behaviour between the code blocks different? Thanks.

The following is the full document.

\documentclass{article}
\usepackage{ragged2e}

\begin{document}

\begin{flushleft}
Step 2:
\end{flushleft}
\begin{center}
chi-squared-test (assuming data is normal)
\end{center}

\raggedright{Step 2:}
\newline
\centering{chi-squared-test (assuming data is normal)}

\end{document}

2 Answers 2

6

\raggedright and \centering don't take an argument. They are switches that affect paragraphs. So you need to group them and end the paragraph first:

\documentclass{article}
\usepackage{ragged2e}

\begin{document}

\begin{flushleft}
Step 2:
\end{flushleft}
\begin{center}
chi-squared-test (assuming data is normal)
\end{center}

{\raggedright Step 2: \par}

{\centering chi-squared-test (assuming data is normal)\par}

\end{document}

enter image description here

1
  • 1
    Didn't realize I was using them wrong. Thanks
    – Jaan
    Commented Nov 27, 2022 at 18:35
2

With flushleft followed by center you get too much vertical space.

Using \raggedright and \centering would not add unwanted space, but they aren't commands with argument. There's another aspect to take into account, though: you don't want page breaks between the two lines, but TeX is always willing to break a page between paragraphs.

You can typeset the thing as a single paragraph, with the bonus of additional markup, which is a good thing, because you might change your mind about the typesetting of such objects and littering your typescript with formatting instruction is evil.

If we make a single two-line paragraph, we can tell TeX that there is an infinite penalty for breaking a page between those lines. We want that the first line is typeset flush left and the second one centered, so we can set the \leftskip and the \rightskip to infinitely stretchable glue and override it with “more infinite” glue in the first line.

I also add some vertical space above and below each step.

\documentclass{article}

\newcommand{\step}[2]{% #1 = step number, #2 = centered text
  \par
  \addvspace{\topsep}% some vertical space above
  \begingroup % make local settings
  \interlinepenalty=10000 % no page break between lines
  \setlength{\parindent}{0pt}% no indentation in the first line
  \setlength{\parfillskip}{0pt}% don't flush left the last line
  \leftskip=0pt plus 1fil  % infinitely stretchable glue at the left
  \rightskip=0pt plus 1fil % infinitely stretchable glue at the right
  Step #1\hfill\break % override those glues in the first line
  #2\par % the second line (centered)
  \endgroup
  \addvspace{\topsep}% some vertical space
}

\begin{document}

\step{1}{chi-squared-test (assuming data is normal)}

\step{2}{chi-squared-test (assuming data is normal) and some words}

\step{3}{chi-squared-test}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .