Text FormattingBest practice to define a command for horizontal space?

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
kornaros
Posts: 40
Joined: Wed Nov 30, 2011 12:11 am

Best practice to define a command for horizontal space?

Post by kornaros »

I try to make a useful command that enters a horizontal space of any length(possitive or negative). Something like

Code: Select all

\newcommand{\myhorspace}[1][0.1pt]{%
\hspace{#1}%
%}
I do not if \hspace is test practice for such commands and for any use(text or math)!

Maybe it is better to define \myhorspace using \kern command but I do not know if it will work well for maths. Also I do not know if \kern and \hspace are related or are equivalent in general.
Could you please give me some advice?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Best practice to define a command for horizontal space?

Post by Johannes_B »

Probably, hspcace* is what you are looking for.

It also works at the beginning of a line.

But i fear, i am givng you bad advice, because this might be an XY question and you just mention the way you think is best to get there.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
kornaros
Posts: 40
Joined: Wed Nov 30, 2011 12:11 am

Re: Best practice to define a command for horizontal space?

Post by kornaros »

Thanks Johannes_B! I am confused sometimes because there many ways to do the same thing in LaTeX! For example, there are many ways for horizontal or vertical space but it is hard to see which is more configurable.. Anyway ...maybe you are right. Do you know if kern and hspace are somehow equivalent for no-math texts?
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Best practice to define a command for horizontal space?

Post by Johannes_B »

Code: Select all

\DeclareRobustCommand\hspace{\@ifstar\@hspacer\@hspace}
\def\@hspace#1{\hskip #1\relax}
\def\@hspacer#1{\vrule \@width\z@\nobreak
                \hskip #1\hskip \z@skip}
hskip as well as kern are TeX primitives (which LaTeX is based on). So, that tells us they are different.

hskip, meaning some horizontal skipping

The implementation of kern from tex.web:

Code: Select all

@ A |kern_node| has a |width| field to specify a (normally negative)
amount of spacing. This spacing correction appears in horizontal lists
between letters like A and V when the font designer said that it looks
better to move them closer together or further apart. A kern node can
also appear in a vertical list, when its `|width|' denotes additional
spacing in the vertical direction. The |subtype| is either |normal| (for
kerns inserted from font information or math mode calculations) or |explicit|
(for kerns inserted from \.{\\kern} and \.{\\/} commands) or |acc_kern|
(for kerns inserted from non-math accents) or |mu_glue| (for kerns
inserted from \.{\\mkern} specifications in math formulas).
So, kern has a completely different purpose.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
kornaros
Posts: 40
Joined: Wed Nov 30, 2011 12:11 am

Re: Best practice to define a command for horizontal space?

Post by kornaros »

Thanks Johannes_B for this useful information!
Post Reply