13

We know that \qquad equals \quad\quad, then is there a way to define a command like \myquad{5} to produce five \quad?

I think there are two way:

  1. get the length of \quad, then \newcommand{\myquad}[1]{\hspace{#1*lengthofquad}}

  2. define a function and load it recursively, but I don't know how to do this.

14

\quad sets a length of 1em, with multiples X of it being of length Xem. So we can define \myquad[<num>] with <num> being optional (default of 1), to produce a space of <num>em:

enter image description here

\documentclass{article}

\newcommand{\myquad}[1][1]{\hspace*{#1em}\ignorespaces}

\begin{document}

x \quad x \par
x \myquad[1] x

x \quad\quad\qquad x \par
x \myquad[4] x

x \qquad\qquad\qquad\qquad\qquad x \par
x \myquad[10] x

\end{document}

We use \hspace* to insert a space regardless of whether you're at the start, middle or end of a rule.

  • works like a charm!! – user19832 Sep 21 '16 at 3:36
  • 4
    note that this freezes the meaning to the length of 1em in the preamble. so \myquad unlike \quad will not depend on the current font, I think not having the \setlength and then \newcommand{\myquad}[1][1]{\hspace*{#1em}\ignorespaces} gives a more natural interpretation. (@user19832) – David Carlisle Sep 21 '16 at 7:58
  • @DavidCarlisle: True. Thanks, I've updated my answer to reflect this. – Werner Sep 21 '16 at 10:02
7

Here is version using a loop:

\documentclass{article}
\usepackage{pgffor}
\newcommand\Quad[1][1]{\foreach \Quaddy in {1,...,#1}{\quad}\ignorespaces}

\begin{document}

  x \quad x \par
  x \Quad[1] x

  x \quad\quad\qquad x \par
  x \Quad[4] x

  x \qquad\qquad\qquad\qquad\qquad x \par
  x \Quad[10] x

\end{document}

with output:

enter image description here

Personally, I prefer the more direct approach in Werner's answer.

  • I try to use tlmgr install pgffor but not found, do you mean pgf instead? – user19832 Sep 21 '16 at 3:22
  • @user19832 Good point, I should have mentioned this. Thepgffor package is part of the bigger pgf package that allows you to use the for-loops from pgf without pulling in all of the pgf package, which is quite large. If you use tlmgr to install pgf then, I believe, you will also get the package pgffor.sty but you can instead just use \usepackage{pgf} in the MWE above. – Andrew Sep 21 '16 at 6:03
4

You can look up how \quad and \qquad are defined, either in the LaTeX source or by asking LaTeX itself:

This is pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015/Debian) (preloaded format=latex)
 restricted \write18 enabled.
**\show\quad
entering extended mode
LaTeX2e <2016/02/01>
Babel <3.9q> and hyphenation patterns for 81 language(s) loaded.
> \quad=macro:
->\hskip 1em\relax .
 \show\quad

?        

*\show\qquad
> \qquad=macro:
->\hskip 2em\relax .
 \show\qquad

? 

*
! Emergency stop.
 \show\qquad

No pages of output.
Transcript written on texput.log.

In many cases this will place you in a maze of twisty commands, all subtly different, but here it's pretty straightforward. A quad is a horizontal space of 1em (the nominal width of a capital M in the current font).

\newcommand{\myquad}[1]{\hskip #1 em\relax}

or in a more LaTeXish way

\newcommand{\myquad}[1]{\hspace{#1 em}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.