Best alternative to toprule outside table environment

Here is a repro for my problem:

\documentclass[10pt]{article}

\usepackage[
a4paper,
top=2.5cm,
bottom=2.5cm,
left=2.5cm,
right=2.5cm
]{geometry}

\usepackage{booktabs}

\begin{document}
\toprule
\vspace{7pt}
Foo
\vspace{7pt}
\toprule
\end{document}


This gives me an error on both \toprule lines:

Misplaced \noalign


However, overleaf still compiles and displays the PDF (which I don't really understand):

As best I can tell, it's because I'm using it outside the context of a tabular environment.

But how can I achieve exactly the same effect outside a table?

\documentclass[10pt]{article}

\usepackage[
a4paper,
top=2.5cm,
bottom=2.5cm,
left=2.5cm,
right=2.5cm
]{geometry}

\usepackage{booktabs}

\begin{document}
\noindent\makebox[\textwidth]{\rule{\textwidth}{0.4pt}}
\vspace{7pt}
Foo \\
\noindent\makebox[\textwidth]{\rule{\textwidth}{0.4pt}}
\end{document}


But this does not align the line vertically in the same manner:

Can anyone tell me how to do horizontal ruling in the same manner outside a table as in?

You could use a tabularx environment which allows to declare the final width of your tabular (\linewidth in this case).

\documentclass[10pt]{article}

\usepackage[
a4paper,
top=2.5cm,
bottom=2.5cm,
left=2.5cm,
right=2.5cm
]{geometry}

\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{lipsum}

\begin{document}
\lipsum[66]
\begin{tabularx}{\linewidth}{l}
\toprule
Foo \\
\bottomrule
\end{tabularx}
\lipsum[66]
\end{document}


• Thanks, but how would I add space above and below "Foo" so the lines aren't so close? – me-- Apr 16 '19 at 8:16
• Nevermind, I got it. I can use \addlinespace[20pt] – me-- Apr 16 '19 at 8:20

see if this gives expected result:

\documentclass[10pt]{article}
\usepackage[a4paper,
margin=2.5cm]{geometry}

\usepackage{lipsum}

\begin{document}
\lipsum[66]
\noindent\rule[1ex]{\textwidth}{0.4pt}
Foo\\
\rule{\textwidth}{0.4pt}
\lipsum[66]
\end{document}


addendum: with use of tcolorbox:

\documentclass[10pt]{article}
\usepackage[a4paper,
margin=2.4cm]{geometry}
\usepackage{tcolorbox}
\tcbset{mybox/.style={%
colback=white,
boxrule=0pt,
colframe=black,
toprule=1pt, bottomrule=1pt,
boxsep=0mm,
arc=0mm,
left=0pt,right=0mm,top=3mm,bottom=3mm,
}}
\newtcolorbox{mybox}{mybox}

\usepackage{lipsum}

\begin{document}
\lipsum[66]
\begin{mybox}
Foo
\end{mybox}
\lipsum[66]
\end{document}


• Almost, but how do I push the bottom rule down further? The top rule specifies 1ex, but that's to control added space below the rule. How do I control space above the rule? Adding vspace seems to just push stuff down that is after the rule, not the rule itself. – me-- Apr 16 '19 at 8:01