Is using \hspace* essentially the same as \strut\hspace?
The description at \hspace vs. \hspace* seems to suggest that this may be the case, but I was wondering the cases where they might differ.
Is using \hspace* essentially the same as \strut\hspace?
The description at \hspace vs. \hspace* seems to suggest that this may be the case, but I was wondering the cases where they might differ.
\hspace* is also valid at the beginning of a line:
\documentclass{article}
\usepackage[a6paper]{geometry}
\parindent=0pt
\begin{document}
\hrulefill\sloppy
This is a sentence which makes no sense.
\hspace{3cm} And so on \ldots
This is a sentence which makes no sense.
\hspace*{3cm} And so on \ldots
This is a sentence which makes no sense.
\strut\hspace{3cm} And so on \ldots
\end{document}
\strut is only a box with height and depth from ( and no width! The behaviour of \hspace* is similiar to \vspace* for the vertical shift.
No, they're not the same. When you use \hspace*{1cm} (or whatever length), LaTeX transforms this into
\@hspacer{1cm}
which in turn becomes
\vrule \@width \z@ \nobreak \hskip #1\hskip \z@skip
as described in my answer to the question you linked. The rule is as high and deep as the horizontal box that eventually it is typeset in.
On the contrary, \strut does \unhcopy\strutbox, that is, it delivers the contents of a box register that also contain a zero width rule, but such that the sum of height and depth equals the (natural size of the) current baseline skip. The box register's contents is updated whenever a font size changing command (such as \normalsize, \footnotesize, \large or even \fontsize{...}{...}\selectfont) is processed, so the height and depth of \strut always (well, almost) reflects the current baseline skip. LaTeX divides the amount in 7 to 3 proportion, so if the baselineskip is 12pt, the height and depth of the strut are 8.4pt and 3.6pt respectively. See Definition of \strut explained for a subtle point about \strut, but it is relevant only if math mode is concerned.
That's the difference: the height and depth of a line where \hspace* appears is only influenced by the letters in it, more precisely by the boxes in it; with \strut\hspace{1cm} you get height and depth whose sum is at least equal to 1\baselineskip (it can be bigger if unusually large boxes are contained in the list).
Moreover, \strut\hspace{1cm} has a feasible break point between the strut and the glue, which \hspace*{1cm} hasn't. Another difference is that \strut\hspace{1cm} will disappear at the end of a paragraph (not the strut, but the glue), because TeX does \unskip there; on the other hand \hspace*{1cm} will not disappear even if \unskip is applied, because of the trailing \hskip\z@skip glue, so \unskip will cancel this and not the main glue.
\strut\hspacewould work at the beginning of a line i.e. that thestrutwould make thehspaceno longer at said beginning of line. :) This is helpful – I was clearly confused about what the strut does. I'm also curious about the case of\hspace{1em}\strut\hspace{1em}– Brian M. Hunt Jul 28 '15 at 13:54\strutis the same as an invisible character/word. With\hspace{1em}\strut\hspace{1em}it is possible to split the the space if\strutappears at the beginning of a line, then the second\hspace{1em}is used. – Jul 28 '15 at 13:57strutto determine whether it is considered present/existent from the layout perspective. Unexpected, but not really surprising. – Brian M. Hunt Jul 28 '15 at 14:11\strutdoes have the effect of stopping hspace being dropped at the beginning of a line, the main difference, as shown in egreg's answer with\hspace*is that as a\struthas a vertical size it can affect vertical position if it ends up being the largest thing on that line. – David Carlisle Jul 28 '15 at 16:49