Losing alignment with fbox

I am using this command to put a box around stuff.

\newcommand{\cfbox}[1]{%
{\color{black}%
\setlength\fboxsep{0pt}\fbox{
\begin{varwidth}{\dimexpr\columnwidth-2}
\leavevmode{\color{black}#1}
\end{varwidth}
}
}%
}


The problem is that each block gets a different alignment, I wanted all of them to be aligned with the first block.

Also, not the core of the question but if anyone also knows: How do I center the caption of the table back? After using the command the text went to the left... (it is not necessary to answer this though).

Here's the example to run

\documentclass{acmart}
\usepackage[utf8]{inputenc}
\usepackage{varwidth}
\usepackage{tikz}

\newcommand{\cfbox}[1]{%
{\setlength\fboxsep{0pt}\fbox{
\begin{varwidth}{\dimexpr\columnwidth-2}
\leavevmode{\color{black}#1}
\end{varwidth}%
}
}
}

\begin{document}

\cfbox{
\cfbox{
\section{Introduction}
}
\cfbox{
If you are new to publishing with ACM, this document is a valuable
guide to the process of preparing your work for publication. If you
have published with ACM before, this document provides insight and
instruction into more recent changes to the article template.
}
}

\cfbox{
\begin{table}[H]
\caption{Frequency of Special Characters}
\label{tab:freq}
\cfbox{
\begin{tabular}{ccl}
\toprule
\midrule
\O & 1 in 1,000& For Swedish names\\
$\pi$ & 1 in 5& Common in math\\
\$& 4 in 5 & Used in business\\$\Psi^2_1$& 1 in 40,000& Unexplained usage\\ \bottomrule \end{tabular} } \end{table} } \cfbox{ \begin{table}[H] \caption{Frequency of Special Characters} \label{tab:freq} \cfbox{ \begin{tabular}{ccl} \toprule Non-English or Math&Frequency&Comments\\ \midrule \O & 1 in 1,000& For Swedish names\\$\pi$& 1 in 5& Common in math\\ \$ & 4 in 5 & Used in business\\
$\Psi^2_1$ & 1 in 40,000& Unexplained usage\\
\bottomrule
\end{tabular}
}
\end{table}
}

\end{document}

• Welcome to TeX.SE. I think you are missing some trailing %, but without a fully compilable MWE including \documentclass and the appropriate packages that reproduces the problem, it is difficlut to test. Try replacing \fbox{ with \fbox{% for instance. Probably need some more after a trailing }. – Peter Grill Jun 9 '20 at 23:12
• @DavidCarliste I use another command equal to this one which uses the color white for the border, so i don't really know if by not adding color black on the first line and getting it by default would change the spacing (since I want both commands to be exactly the same, but with different colors). I also changed the document class on the example thanks for telling me that. – Pyrrhic Buddha Jun 9 '20 at 23:26
• I fixed, it is running now. But on this example, separated from the rest of the context of my latex, there is no problem on the alignment! I will be now trying to add the other things from my original latex until I find what it changing the alignment, so the people can help me. Sorry to be such a newbie tho. – Pyrrhic Buddha Jun 9 '20 at 23:36
• Your example produces errors ! Illegal unit of measure (pt inserted). <to be read again> \relax l.27 } ?  after any error you should not even look at the pdf output it is not intended to be usable just a debugging aid at best. \dimexpr\columnwidth-2 is a syntax error (no units on the 2 ) but I can not guess what expression you intended there. – David Carlisle Jun 9 '20 at 23:57
• @DavidCarlisle I went through a lot of snippets copying and pasting and when it worked I didn't really know what parts were working... I took out the -2 and less errors appeared on my main latex! Thanks! – Pyrrhic Buddha Jun 10 '20 at 0:09

I found the answer, having maketitle on the tex impacts the alignment of some specific cfbox. If you have maketitle, then between cfbox of table and section there shouldn't be a blank line, otherwise the alignment changes. On the other hand, if you don't have maketitle on the tex, then you need to have a blank line between cfbox of table and section, otherwise the alignmnent changes to the opposite direction. The comments said I should put % on the cfbox, but after putting % everywhere on the tex, there was no change.

I still don't know how to center the table description. This tex below shows the example running as expected when with maketitle.

\documentclass[acmsmall, natbib=false, review]{acmart}

\usepackage{varwidth}

\acmYear{2018}
\acmDOI{10.1145/1122445.1122456}
\acmConference[Woodstock '18]{Woodstock '18: ACM
Symposium
on Neural
Gaze Detection}{June 03--05, 2018}{Woodstock, NY}
\acmBooktitle{Woodstock '18: ACM Symposium on Neural
Gaze
Detection,
June 03--05, 2018, Woodstock, NY}
\acmPrice{15.00}
\acmISBN{978-1-4503-XXXX-X/18/06}

\newcommand{\cfbox}[1]{%
{\setlength\fboxsep{0pt}\fbox{%
\begin{varwidth}{\dimexpr\columnwidth}%
{\leavevmode\color{black}#1}%
\end{varwidth}%
}%
}
}

\begin{document}

\title{Test}

\maketitle

\cfbox{

\cfbox{
\section{Introduction}
}

\cfbox{
If you are new to publishing with ACM, this document is
a valuable
guide to the process of preparing your work for
publication.
If you
have published with ACM before, this document provides
insight and
instruction into more recent changes to the article
template.
}

}

\cfbox{
\begin{table}[H]
\caption{Frequency of Special Characters}
\label{tab:freq}
\cfbox{
\begin{tabular}{ccl}
\toprule
\midrule
\O & 1 in 1,000& For Swedish names\\
$\pi$ & 1 in 5& Common in math\\
\$& 4 in 5 & Used in business\\$\Psi^2_1\$ & 1 in 40,000& Unexplained usage\\
\bottomrule
\end{tabular}
}
\end{table}
}
\cfbox{
\cfbox{
\section{Introduction}
}

\cfbox{
If you are new to publishing with ACM, this document is
a valuable
guide to the process of preparing your work for
publication.
If you
have published with ACM before, this document provides
insight and
instruction into more recent changes to the article
template.
}

}

\cfbox{

\cfbox{
\section{Introduction}
}

\cfbox{
If you are new to publishing with ACM, this document is
a valuable
guide to the process of preparing your work for
publication.
If you
have published with ACM before, this document provides
insight and
instruction into more recent changes to the article
template.
}

}

\cfbox{

\cfbox{
\section{Introduction}
}

\cfbox{
If you are new to publishing with ACM, this document is
a
valuable
guide to the process of preparing your work for
publication.
If you
have published with ACM before, this document provides
insight and
instruction into more recent changes to the article
template.
}

}

\cfbox{

\cfbox{
\section{Introduction}
}

\cfbox{
If you are new to publishing with ACM, this document is
a
valuable
guide to the process of preparing your work for
publication.
If you
have published with ACM before, this document provides
insight and
instruction into more recent changes to the article
template.
}

}

\end{document}
\endinput