3

How do I left align the text and align these equal signs? As for the equal sign I used \, and it kind of works but I want to be more close. Is there a smaller spacing than this or any way to better align these equal signs?

enter image description here


The expected output is something like this.

enter image description here

\documentclass{article}
\usepackage[utf8]{inputenc}
\begin{document}

Input data \,\, =  2000\\
Deleted data \,\,\,\,\,\,\,\,= 500\\
Remaining data = 2000 - 500 =1500   


\end{document}

Sorry that I probably should have mentioned that I'm going to put the text inside a table like this. The solution @Mico gave works but not in this table. So I think the table change the formating somehow.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\begin{document}

\begin{table}[]
\centering
\caption{This is a table}
\label{tab: this is a table} 
\begin{tabular}{cc} \\ \toprule
\textbf{No}      & \textbf{Example}  \\ \midrule
1         & 9                         \\
2         & 5                      \\
3         & 3                      \\ \midrule
\multicolumn{2}{l}{\begin{tabular}[c]{@{}l@{}}
\[
\setlength\arraycolsep{0pt}
\begin{array}{ L @{{}={}} l }
\text{Input data}     &  2000 \\
\text{Deleted data}   &  500  \\
\text{Remaining data}  &  2000-500=1500
\end{array}
\]
\end{tabular}} \\ \bottomrule
\end{tabular}
\end{table}

\end{document}
3
  • 1
    What prevents you from using a tabular? Aug 10, 2022 at 18:01
  • 1
    @JasperHabicht actually I'm going to put this in a cell of a table.
    – hana
    Aug 10, 2022 at 18:04
  • Now that you edited your question: You already nest tabulars, so why not just use a tabular with two columns that is placed in the cell? Aligning things will be very easy then. Aug 10, 2022 at 18:26

4 Answers 4

4

Here's a different array-based solution.

enter image description here

\documentclass{article}
\usepackage{array} % for \newcolumntype macro
\newcolumntype{L}{>{$}l<{$}}

\begin{document}
\[
\setlength\arraycolsep{0pt}
\begin{array}{ L @{{}={}} l }
Input data     &  2000 \\
Deleted data   &  500  \\
Remaning data  &  2000-500=1500
\end{array}
\]
\end{document}

Addendum: Here's how I would modify the OP's addtional example code to embed the array inside a two-column tabular environment:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs,amsmath,array}
\newcolumntype{L}{>{$}l<{$}}

\begin{document}

\begin{table}[h]
\centering
\caption{This is a table\strut}
\label{tab:this is a table} 
\begin{tabular}{@{} cc @{}} 
\toprule
\textbf{No} & \textbf{Example} \\ 
\midrule
1  & 9  \\
2  & 5  \\
3  & 3  \\ 
\midrule
\multicolumn{2}{@{}c@{}}{%
   \setlength\arraycolsep{0pt}$
   \begin{array}{ L @{{}={}} l }
   Input data     &  2000 \\
   Deleted data   &  500  \\
   Remaining data &  2000-500=1500
   \end{array}$
} \\ 
\bottomrule
\end{tabular}
\end{table}

\end{document}
5
  • 1
    Could you check the my updated post? Your solution does work well alone but it's my fault that I didn't mention it clearly that I'm going to put it in a table.
    – hana
    Aug 10, 2022 at 18:19
  • @hana - If you (a) change from display math to inline math, i.e., if you replace \[ and \] with $, and (b) place \setlength\arraycolsep{0pt} before the initial $ symbol, this method works fine inside a cell of a tabular environment.
    – Mico
    Aug 10, 2022 at 18:23
  • @hana - Please see the addendum I just posted.
    – Mico
    Aug 10, 2022 at 18:31
  • Thanks, it works with table now.
    – hana
    Aug 10, 2022 at 19:17
  • The "accept" vote on this answer, placed a short while ago, raised my total rep count on TeX.SE to exactly 444,000 points. Many thanks!!!
    – Mico
    Aug 17, 2022 at 20:29
4

Rarely used but not to be forgotten: the tabbing environment. Inside a tabular, you would need to wrap it in a \parbox or similar, however.

(I placed the numbers in math mode for nicer typsetting.)

\documentclass{article}
\begin{document}

\begin{tabbing}
Remaining data \= \kill      % needed to define the position of the tab
Input data     \>$= 2000$ \\
Deleted data   \>$= 500$  \\
Remaining data \>$= 2000 - 500 = 1500$ 
\end{tabbing}

\end{document}

enter image description here


Edit

Given the fact that you want to place this inside a table cell that aready has a nested tabular inside, why not just do the following:

\documentclass{article}
\usepackage{booktabs}

\begin{document}

\begin{table}
\centering
\caption{This is a table}
\label{tab: this is a table} 
\begin{tabular}{cc} \\ \toprule
\textbf{No}  & \textbf{Example}  \\ \midrule
1            & 9                 \\
2            & 5                 \\
3            & 3                 \\ \midrule
\multicolumn{2}{l}{
    \begin{tabular}[c]{@{}ll@{}}
    Input data      & $2000$ \\
    Deleted data    & $500$  \\
    Remaining data  & $2000-500=1500$
    \end{tabular}
}                                \\ \bottomrule
\end{tabular}
\end{table}

\end{document}

enter image description here

You can use the p{} column type if you want to specifiy the with of a certain column:

\documentclass{article}
\usepackage{booktabs}

\begin{document}

\begin{table}
\centering
\caption{This is a table}
\label{tab: this is a table} 
\begin{tabular}{cc} \\ \toprule
\textbf{No}  & \textbf{Example}  \\ \midrule
1            & 9                 \\
2            & 5                 \\
3            & 3                 \\ \midrule
\multicolumn{2}{l}{
    \begin{tabular}[c]{@{}p{4cm}l@{}}
    Input data      & $2000$ \\
    Deleted data    & $500$  \\
    Remaining data  & $2000-500=1500$
    \end{tabular}
}                                \\ \bottomrule
\end{tabular}
\end{table}

\end{document}

enter image description here

5
  • Your suggestion looks good too.
    – hana
    Aug 10, 2022 at 19:18
  • If I want to add more space between the text and the number in the last 3 lines, how would you do it? I tried add \text{ } but it doesn't change anything.
    – hana
    Aug 12, 2022 at 13:51
  • 1
    @hana Consecutive spaces will be reduced (except for verbatim environments). If you want to enlarge the left column, better change its type from l to p{4cm} or something similar. Aug 12, 2022 at 15:27
  • I think you misundertood my question. In this table your code looks very nice. However, in some table where the second columns data become long so I want to add more space between for example input data and 2000 so to push the 2000 further away to make it look nicer.
    – hana
    Aug 12, 2022 at 19:00
  • 1
    @hana See my edit. But maybe this is not what you want. But then, maybe one of the other answers is better suited for your needs. Aug 15, 2022 at 18:26
3

This uses a tabular and @{$\null =\null$} to add correctly spaced equal signs between the column.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\begin{document}

\begin{table}[]
\centering
\caption{This is a table}
\label{tab: this is a table} 
\begin{tabular}{cc} \\ \toprule
\textbf{No}      & \textbf{Example}  \\ \midrule
1         & 9                         \\
2         & 5                      \\
3         & 3                      \\ \midrule
\multicolumn{2}{l}{\begin{tabular}{@{}l@{$\null =\null$}l}
Input data     &  2000 \\
Deleted data   &  500  \\
Remaining data  &  $2000-500=1500$
\end{tabular}} \\ \bottomrule
\end{tabular}
\end{table}

\end{document}
2
  • +1. The LaTeX kernel defines\null as \hbox{}. Could you comment on the difference between your solution for the inter-column element, @{\hbox{}=\hbox{}}, and the one I used in my answer, viz., @{{}={}}?
    – Mico
    Aug 10, 2022 at 21:30
  • @Mico - Mostly I didn't really notice the similarity. I regard {} more as \begingroup\endgroup. Adding \null around math operators is fairly common practice. Aug 11, 2022 at 3:35
1

You could use the array environment, which only works in math mode.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
\begin{array}{ll}
    \text{Input data} &=  2000 \\
    \text{Deleted data} &= 500 \\
    \text{Remaining data} &= 2000 - 500 = 1500
\end{array}
\]
\end{document}
3
  • nice, it works.
    – hana
    Aug 10, 2022 at 18:04
  • 1
    With this solution, the amount of whitespace around the = symbols is quite asymmetric. It is most easily visible in the third row.
    – Mico
    Aug 10, 2022 at 18:05
  • you need l@{}l here Aug 10, 2022 at 18:21

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.