1

This example writes three lines, each of which have four numbers separated by an equal length of space.

\documentclass{article}
\begin{document}
\noindent
\thispagestyle{empty}%
1\hfill2\hfill3\hfill4\\5\hfill6\hfill7\hfill8\\9\hfill10\hfill11\hfill12
\end{document}
1 (SPACE) 2 (SPACE) 3 (SPACE) 4
5 (SPACE) 6 (SPACE) 7 (SPACE) 8
9 (SPACE) 10 (SPACE) 11 (SPACE) 12

The line of code operates normally under math mode.

\documentclass{article}
\begin{document}
\noindent
\thispagestyle{empty}%
$1\hfill2\hfill3\hfill4\\5\hfill6\hfill7\hfill8\\9\hfill10\hfill11\hfill12$
\end{document}
1 (SPACE) 2 (SPACE) 3 (SPACE) 4
5 (SPACE) 6 (SPACE) 7 (SPACE) 8
9 (SPACE) 10 (SPACE) 11 (SPACE) 12

Now I want the three lines to be spread out across the page with equal spacing inbetween. However when I change the newlines to \vfill I get an error:

\documentclass{article}
\begin{document}
\noindent
\thispagestyle{empty}%
$1\hfill2\hfill3\hfill4\vfill5\hfill6\hfill7\hfill8\vfill9\hfill10\hfill11\hfill12$
\end{document}
! Missing $ inserted.
<inserted text> 
                $
l.5 $1\hfill2\hfill3\hfill4\vfill
                                 5\hfill6\hfill7\hfill8\vfill9\hfill10\hfill...

Only when I put the \vfills out of math mode will it compile:

\documentclass{article}
\begin{document}
\noindent
\thispagestyle{empty}%
$1\hfill2\hfill3\hfill4$\vfill$5\hfill6\hfill7\hfill8$\vfill$9\hfill10\hfill11\hfill12$
\end{document}
1 (SPACE) 2 (SPACE) 3 (SPACE) 4
(SPACE)
5 (SPACE) 6 (SPACE) 7 (SPACE) 8
(SPACE)
9 (SPACE) 10 (SPACE) 11 (SPACE) 12

Using amsmath's \text to force textmode did not help.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\noindent
\thispagestyle{empty}%
$1\hfill2\hfill3\hfill4\text{\vfill}5\hfill6\hfill7\hfill8\text{\vfill}9\hfill10\hfill11\hfill12$
\end{document}

How come \hfill works fine in math mode while \vfill does not? Is there a workaround to make it usable in math mode? When typing manually I could just open and close math mode eveytime I need a \vfill, but when using sagetex the output is printed using $\sagestr{some_custom_functions(some_variable)}$, which means that everything the function outputs must be usable in mathmode.

5
  • "Missing $ inserted" mean that expect only \vfill out of the math mode, the same that if you try $1\textbf{foo}2$ instead of $1$ \textbf{foo} $2$.
    – Fran
    Jul 18, 2023 at 16:57
  • 3
    vfill can not work in horizontal mode: hbox, mbox, math, ... Jul 18, 2023 at 17:20
  • @Fran $1\textbf{foo}2$ is not an error Jul 18, 2023 at 17:21
  • @DavidCarlisle Opps, what bad example! I would be thinking about \paragraph or so... :(
    – Fran
    Jul 18, 2023 at 17:30
  • Welcome, @zhsm54983
    – MS-SPO
    Jul 18, 2023 at 17:57

3 Answers 3

3

$ is inline math, which is why vfill won't work.

But you can also use an array to do something similar. Here's an MWE:

\documentclass{article}

\begin{document}

\[\arraycolsep=10pt\def\arraystretch{3}
\begin{array}{ccc}
    1 & 2 & 3 \\ 
    4 & 5 & 6 \\ 
    7 & 8 & 9
\end{array}
\]

\end{document}

The arraycolsep is your hfill and the arraystretch is your vfill. You can adjust these values as necessary.

enter image description here

You can also add extra vertical space to just one line by adding something like [30pt] after the line break in the array:

\documentclass{article}

\begin{document}

\[\arraycolsep=10pt\def\arraystretch{3}
\begin{array}{ccc}
    1 & 2 & 3 \\[30pt]
    4 & 5 & 6 \\ 
    7 & 8 & 9
\end{array}
\]

\end{document}

enter image description here

1

The \vfill primitive inserts \par before it if it is in horizontal mode and it causes error in math mode. TeX suggests to close the math mode by inserting $ and \vfill is "to be read again" as we can read in the error message.

You can insert your \vfill in the \vadjust parameter. This primitive waits to finishing the paragraph and then inserts the vertical material from its parameter between lines of created paragraph. Your example can look like this:

\noindent
$1\hfill2\hfill3\hfill4\vadjust{\vfill}\break
 5\hfill6\hfill7\hfill8\vadjust{\vfill}\break
 9\hfill10\hfill11\hfill12$
0

\vfill is illegal in math mode, as you discovered. But you can use \vspace{\fill} instead.

\documentclass{article}
\usepackage[a6paper,showframe]{geometry}% for a smaller picture

\begin{document}

\thispagestyle{empty}

\noindent
$1\hfill2\hfill3\hfill4\vspace{\fill}\\
5\hfill6\hfill7\hfill8\vspace{\fill}\\
9\hfill10\hfill11\hfill12$

\end{document}

I used a5paper just to get a smaller picture and showframe for showing the boundaries of the text block.

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .