1

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):

using toprule

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?

I found this answer, which leads to code like this:

\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:

using rule

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

me--
  • 377
  • 2
  • 8

2 Answers2

1

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}

enter image description here

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}

enter image description here

Zarko
  • 296,517
  • 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 08:01
1

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}

enter image description here

Ignasi
  • 136,588