[Tex/LaTex] Widths of digits and letters

horizontal alignment

Within a minipage environment (of a fixed length) I have a list; each list item consists of a phrase followed by an equal sign followed by a percent. The first percent is 60%.

I want (1) each phrase left aligned (within the minipage) (2) equal signs aligned, (3) the percent is right aligned (within the minipage) and (4) have only one inter word space between the equal sign and the first percent.

In the following attempt, I have tasks (1), (3) and (4) completed but not (2).

\documentclass[12pt]{report}
\begin{document}
\begin{flushleft}
\begin{minipage}{4in}
Midterm Exam \hfill= 60\% \\
Home-work \hfill= 5\% \\
Quizzes\hfill= s\% \\
Report \hfill= S\% \\
Final Exam\hfill= SS\% 
\end{minipage}
\end{flushleft}
\end{document}

enter image description here

After spending some time trying to figure out widths of letters and digits, and then spending some more time here at TeX.SX I found a solution. (The following solution also provides the widths of some letters and a digit as a bonus.)

\documentclass[12pt]{report}
\begin{document}
\begin{flushleft}

\newlength{\six}
\settowidth{\six}{6}

\newlength{\s}
\settowidth{\s}{s}

\newlength{\eS}
\settowidth{\eS}{S}

\begin{minipage}{4in}
Midterm Exam \hfill= 60\% \\
Home-work \hfill= \hspace{\six}5\% \\
Quizzes\hfill= \hspace{-\s}\hspace{\six}\hspace{\six}s\% \\
Report \hfill= \hspace{-\eS}\hspace{\six}\hspace{\six}S\% \\
Final Exam\hfill= \hspace{-\eS}\hspace{-\eS}\hspace{\six}\hspace{\six}SS\% \\
\end{minipage}

The width of S = \the\eS\\
The width of 6 = \the\six\\
The width of s = \the\s

\end{flushleft}
\end{document}

enter image description here

My question is:

Is this the best way to do this? I assume not. Then what would be the best way to do this?

(P.s. I tried a tabular environment within the minipage environment. It introduces an left indent that I could not get rid of.)

Best Answer

I tried a tabular environment within the minipage environment. It introduces an left indent that I could not get rid of.

The length that controls the horizontal cell padding in tabular environments is \tabcolsep. If you reset this length to 0pt inside the minipage, the effects will be local to that minipage. You can also use a tabularx environment instead of a normal tabular as it allows a specified width and provides the X column specifier which will expand a given column to fill any leftover space.

\documentclass[12pt]{report}

\usepackage{lipsum}
\usepackage{tabularx}
\usepackage{tikz}

\begin{document}
\lipsum[1]
\medskip

\noindent\begin{minipage}{4in}
  \setlength\tabcolsep{0pt}

  \begin{tabularx}{\linewidth}{X@{ = }r}
    Midterm Exam & 60\% \tabularnewline
    Home-work & 5\% \tabularnewline
    Quizzes & s\% \tabularnewline
    Report & S\% \tabularnewline
    Final Exam & SS\%
  \end{tabularx}
\end{minipage}
% Length check
\\\tikz{\draw[|-|] (0,0) -- node[fill=white]{4in} (4in, 0);}

\medskip
\lipsum[2]
\end{document}

Results:

Tabular with no horizontal padding

Related Question