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:

migrated from stackoverflow.com Jul 12 '16 at 15:37

This question came from our site for professional and enthusiast programmers.

  • Could you provide an image of what you mean? – Werner Jul 11 '16 at 4:33
  • @Werner I added a sample text which is written in MS Word. The preceding space of second line is reduced. – Mahdi Jul 11 '16 at 4:40
  • Can you also show the command usage that would provide the required output? – Werner Jul 11 '16 at 5:17
  • @Werner Sure. It would be something like \main {This text is written using command main} \mainv {This text is written using command mainv} – Mahdi Jul 12 '16 at 15:18
  • How is the macro \main defined? – Mico Jul 12 '16 at 18:32
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

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

  • Learn something everyday :) – Jonathan Komar Jul 12 '16 at 19:29
  • Thanks Gustavo. I'll try this solution later today, but I guess it does what I'm looking for. – Mahdi Jul 13 '16 at 15:47
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. – Mahdi 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

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.