1

Simply put, let's say I want to right-align a specific column in alignat*. How would I do so?

Example:

\documentclass{article}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{enumitem}

\newcounter{eqn_resetter}
\newcounter{eqn}[eqn_resetter]
\renewcommand*{\theeqn}{\arabic{eqn}.}
\newcommand{\num}{\refstepcounter{eqn}\text{\theeqn}\quad}

\newcommand{\prooflinen}[3][]{&\num #2 &&\qquad\text{#3}#1 \\}
\newcommand{\quadnum}[1][1]{\hspace*{#1em}\ignorespaces}
\newcommand{\algorithmline}[3]{\prooflinen{\quadnum[#1]\text{#2}}{$#3$}}

\begin{document}

        \begin{alignat*}{2}
            &\text{\textbf{Statement}} &&\qquad\text{\textbf{Number of times run}} \\
            \algorithmline{0}{power\_level = 10000}{1}
            \algorithmline{0}{k = -10}{1}
            \algorithmline{0}{for i = 1 to moves.length}{n + 1}
            \algorithmline{2}{uno = moves[i] + 8}{n}
            \algorithmline{2}{while k $<$ moves.length}{2n + 11}
            \algorithmline{4}{k += 1}{n + 11}
        \end{alignat*}

\end{document}

Gives

LaTeX picture

How can I right align the text under Number of times run?

  • 3
    The alignat environment uses the column alignment rlrl. So you have to ensure that the column titled »Number of times run« is an r column: change the && to & in the definition of \prooflinen and in the line with the column titles (first line of your alignat* environment). – esdd Feb 28 at 12:51
  • @esdd That is exactly what I needed! If you put an answer below, I will accept it. – Gigi Bayte 2 Feb 28 at 19:39
1

alignat is the wrong tool; you need a tabular.

\documentclass{article}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{enumitem}

\newcounter{baytealgoline}
\newcommand{\numberalgoline}{\refstepcounter{baytealgoline}\thebaytealgoline.\quad}

\newcommand{\prooflinen}[3][]{\numberalgoline #2 &#3$#1$ \\}
\newcommand{\quadnum}[1][1]{\hspace*{#1em}\ignorespaces}
\newcommand{\algorithmline}[3]{\prooflinen{\quadnum[#1]\texttt{#2}}{$#3$}}

\newenvironment{baytealgo}[1][]
 {% don't reset the number if the optional argument is 'continue'
  \if\relax\detokenize{#1}\relax\setcounter{baytealgoline}{0}\fi
  \begin{tabular}{@{}l@{\qquad}r@{}}}
 {\end{tabular}}

\begin{document}

\begin{center}
\begin{baytealgo}
\textbf{Statement} & \textbf{Number of times run} \\
\algorithmline{0}{power\_level = 10000}{1}
\algorithmline{0}{k = -10}{1}
\algorithmline{0}{for i = 1 to moves.length}{n + 1}
\algorithmline{2}{uno = moves[i] + 8}{n}
\algorithmline{2}{while k $<$ moves.length}{2n + 11}
\algorithmline{4}{k += 1}{n + 11}
\end{baytealgo}
\end{center}

\end{document}

enter image description here

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.