2

I'm trying to define a new command similar to

\newcommand {\mainv} {\par\noindent\hspace*{0pt}\ignorespaces}

However, I want to have some negative preceding vertical space. I know that \vspace adds vertical space after the text. Is there a similar command that can add vertical space before the text?

The command that will provide the following output will be:

\main {This text is written using command main}
\mainv {This text is written using command mainv}

where \main is defined as

\newcommand {\main} {\par\noindent\hspace*{0pt}\ignorespaces}

The following image in MS Word shows what I'm looking for:

Matt
  • 1,107
  • Could you provide an image of what you mean? – Werner Jul 11 '16 at 04:33
  • @Werner I added a sample text which is written in MS Word. The preceding space of second line is reduced. – Matt Jul 11 '16 at 04:40
  • Can you also show the command usage that would provide the required output? – Werner Jul 11 '16 at 05:17
  • @Werner Sure. It would be something like \main {This text is written using command main} \mainv {This text is written using command mainv} – Matt Jul 12 '16 at 15:18
  • How is the macro \main defined? – Mico Jul 12 '16 at 18:32
  • @Mico Sorry for the confusion. I updated the post. – Matt Jul 12 '16 at 19:00
  • @Mahdi: What \documentclass are you using? It could be that the document class defines \parskip to be non-zero, and that's the space that you're seeing. – Werner Jul 12 '16 at 19:01
  • What is the purpose of the \hspace*{0pt}\ignorespaces part of the macro(s)? – Mico Jul 12 '16 at 19:07
  • @Werner That's the default spacing, I wanted the \mainv to have a space less than the default. Willie's answer works for my problem. Thanks a lot for your help. – Matt Jul 12 '16 at 20:23
  • @Mico I don't really know. This is taken from another source. – Matt Jul 13 '16 at 15:45

3 Answers3

3

It is not completely clear to me what you are trying to achieve and why do you want to overlap lines, so I’ll stick to the question “Is there a command that can add vertical space before a line of text?”.

The pdfTeX engine has a \vadjust pre primitive that can conveniently be used for this. See the following MWE.

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\makeatletter

\newcommand*\vspacebeforeline[1]{%
    \ifvmode % if in vertical mode, act as "\vspace{#1}"
        \vskip #1
        \vskip \z@skip
    \else
        \@bsphack
        \vadjust pre {%
            \@restorepar
            \vskip #1
            \vskip \z@skip
        }%
        \@esphack
    \fi
}

\makeatother




\begin{document}

Here is some nonsensical text, written for the sole purpose of filling up at
least one line.  Well, after all I~think that two lines would be even better.
And now, we use our new command exactly here\vspacebeforeline{18pt plus 6pt 
minus 6pt}, followed by a few other words to end the test.

\vspacebeforeline{3pt}

It can be used in vertical mode, too.

\end{document}

This is the output it produces:

Output of the code

GuM
  • 21,558
2

Something like this?

\documentclass{article}

\newcommand\main{\par\noindent\hspace*{0pt}\ignorespaces}
\newcommand\mainV{\vspace{-10pt}\main}

\begin{document}

\main Some text
\main Some text
\main Some text
\mainV some other, overlapping text
\end{document}

Output from the code above

Willie Wong
  • 24,733
  • 8
  • 74
  • 106
2

Unless you are trying to overlap lines, I would venture to guess you're looking for the variable \baselineskip or \baselinestretch? But you probably don't want to change those variables directly because other macros scale off of them.

\baselineskip: A parameter that tells TeX the distance between baselines.

or \linespread

e.g. set line spread for next font change: \linespread{1.5}\selectfont

Otherwise, yeah, just manipulating the space around vertical boxes, \vspace{<natural height> plus <stretching> minus <shrinking>} is your tool.

To get yourself on the right track

  • Thanks a lot for your explanation. There is a lot of useful information in your answer. Sorry I can't up-vote yet. – Matt Jul 12 '16 at 19:05
  • @Mahdi No prob. Yea without a working example from you, it's a shot in the dark, but I see you're new here. – Jonathan Komar Jul 12 '16 at 19:07