10

I am wondering if there is a recommended way of centering text at a certain point in a line of text?

When trying to align monospace characters on multiple consecutive lines, alignment is fine with strings of equal length, with the alignment point in the middle. However, if the strings are of different length and/or the point where the string is to be aligned is off-center it is a bit more challenging.

I managed to do it by adding/subtracting space at the beginning of the lines using \hspace*, but am curious if there is a better way of doing it.

Below is an example. First, there is the characters A-G, with "D" aligned in the center. If the "A" is removed from the first line, and "G" from the second, the text is no longer aligned at the "D". Finally, the last part shows the way I found of realigning at the "D".

\documentclass{article}
\begin{document}
\begin{center}
    \texttt{ABCDEFG}\\
    \texttt{GFEDCBA}
    \par\vspace*{10pt}
    \texttt{BCDEFG}\\
    \texttt{GFEDCB}
    \par\vspace*{10pt}
    \hspace*{5pt}\texttt{BCDEFG}\\
    \hspace*{-6pt}\texttt{GFEDCB}
\end{center}
\end{document}

3 Answers 3

10

I would suggest using a regular tabular environment and using the column alignment character & to indicate your centering or pivot point. For example,

\documentclass{article}
\begin{document}
\ttfamily
\begin{tabular}{r@{}c@{}l}
  ABC&D&EFG \\
  GFE&D&CBA \\[10pt]
   BC&D&EFG \\
  DFE&D&CB
\end{tabular}
\end{document}

table1

Although technically different from @egreg's answer, you could consider & to represent the "padding character" that allows you to align the pivot point. The regular tabular inter-column space has been set to zero using @{}. This ensures that the three columns have no separation between them.

Since this solution now falls within the realm of tabular, all tabular (and array) related things apply. For example, if you which to underline the pivot entry, define a new pivot column C using \newcolumntype from the array package. The following works:

\documentclass{article}
\usepackage{array}% http://ctan.org/pkg/array
\newsavebox{\colbox}
\begin{document}
\newcolumntype{C}{@{}>{\begin{lrbox}{\colbox}}c<{\end{lrbox}\underline{\usebox{\colbox}}}@{}}%
\ttfamily
\begin{tabular}{rCl}
  ABC&DEF&G \\
  G&FED&CBA \\[10pt]
   BC&DEF&G \\
  G&FED&CB
\end{tabular}
\end{document}

table2

Perhaps you're interested in highlight (with some back colour) the pivot column. This is possible using the colortbl package (together with the xcolor package) and (say) black!15 (15% black):

\documentclass{article}
\usepackage{array}% http://ctan.org/pkg/array
\usepackage{colortbl}% http://ctan.org/pkg/colortbl
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\begin{document}
\renewcommand{\tabcolsep}{0pt}
\newcolumntype{C}{@{}>{\cellcolor{black!15}}c@{}}%
\ttfamily
\begin{tabular}{rCl}
  ABC&DEF&G \\
  G&FED&CBA \\[10pt]
   BC&DEF&G \\
  G&FED&CB
\end{tabular}
\end{document}

table3

3

You can define a "padding character"; in the following code I've used ? (it must be a character you don't otherwise use inside the environment):

\documentclass{article}
\newenvironment{ttcenter}[1][?]
  {\begin{center}\catcode`#1=\active
   \scantokens{\def#1{\leavevmode\phantom{A}}}\ttfamily}
  {\end{center}}

\begin{document}
\begin{ttcenter}
ABCDEFG\\
GFEDCBA\\[10pt]
?BCDEFG\\[10pt]
GFEDCB?
\end{ttcenter}
\end{document}

Also the input will be "visually aligned". If you need another character, specify it as the optional argument:

\begin{ttcenter}[-]
ABCDEFG\\
GFEDCBA\\[10pt]
-BCDEFG\\[10pt]
GFEDCB-
\end{ttcenter}

A simpler way, if the "padding" goes only on one side of the letters is

\newenvironment{ttcenter2}
  {\begin{center}\ttfamily\begin{tabular}{@{}c@{}}}
  {\end{tabular}\end{center}}


\begin{ttcenter2}
        ABCDEFG\\
        GFEDCBA\\[10pt]
\hfill   BCDEFG\\[10pt]
        GFEDCB\hfill
\end{ttcenter2}

The "padding character" version is needed if, for instance, there are holes.

2
  • Is it possible to do it such that the padding character expands to make the lines the right length? Rather than specifying the number required? Something along the lines of \hfill thats adds an adjustable amount of space? So that if, for example, one line needs 5 spaces and the other 7 in order to align it as desired, then you would only need to put the padding character once.
    – rossorp
    Oct 4, 2011 at 13:21
  • No, without knowing in advance the maximum width. If the padding always goes on one side of the letters, see the edited answer.
    – egreg
    Oct 4, 2011 at 13:31
2

You could simply use \phantom{} macro to insert space equivalent to the characters that were removed:

\documentclass{article}
\begin{document}
\begin{center}
    \texttt{\phantom{A}BCDEFG}\\
    \texttt{GFEDCB\phantom{A}}
\end{center}
\end{document}

While this will solve the given code example, the solutions provided by @egreg and @Werner provide additional flexibility and may be more appropriate depending on exactly what you need.

You must log in to answer this question.

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