Aligning text off-center
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}
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}
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}
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.
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.