0

I want to change line spacing locally inside a custom command. However, the final renewcommand overwrites line spacing in the custom command. How can I locally set line spacing and prevent it to be overwritten?

I am aware of other methods of setting line spacing but since I am modifying a code which doesn't belong to me, I want to stick to baselinestretch.

test.sty:

\xdef\@singlespaced{1}

\newcommand\lalala{
    \let\baselinestretch\@singlespaced{
        sdfhjsdkfh sfjkhsdjfhs sdfkhskdjfhs dfksdhfjkshdf jkhsdkfjhsdkjf hskjdfhksjfh skjfh skjdfhksjf hskjdfhskjdfh skjfh skjfh skjdfh skjdhf skdf
    }
}

\renewcommand{\baselinestretch}{2}

main.tex:

\documentclass{report}

\usepackage{test}

\begin{document}
    \lalala
\end{document}
  • What is \1? When I run your example, I get an undefined control sequence error. Are you trying to set \baselinestretch to 1? – schtandard Jun 5 at 13:39
  • @schtandard Sorry edited. – Shibli Jun 5 at 13:43
1

You need to set the text size after changing \baselinestretch for it to take effect. For the change to be local, you should put it inside the group, not before it. Since \baselinestretch always affects whole paragraph, you need to make the content of \lalala a separate paragraph from the surrounding text.

\documentclass{report}

\usepackage{lipsum}

\newcommand\lalala{%
    \par
    {\def\baselinestretch{1}\normalsize
        sdfhjsdkfh sfjkhsdjfhs sdfkhskdjfhs dfksdhfjkshdf jkhsdkfjhsdkjf hskjdfhksjfh skjfh skjdfhksjf hskjdfhskjdfh skjfh skjfh skjdfh skjdhf skdf%
        \par
    }%
}

\renewcommand{\baselinestretch}{2}

\begin{document}

\lipsum[1]
\lalala
\lipsum[2]

\end{document}

MWE output

  • Do we need the first \par? I still get single line spacing without it. Also, what do you mean with "make the content of \lalala one"? – Shibli Jun 5 at 13:58
  • What I meant was that we need to make the content of \lalala a separate paragraph from the surrounding text. I changed the wording accordingly. This is why the first \par is necessary; otherwise the whole paragraph (including \lipsum[1]) is set with a \baselinestretch of 1. – schtandard Jun 5 at 14:02
  • By the way, if you are unhappy with the uneven spacing around the tighter paragraph, you can put it inside a \parbox or minipage. – schtandard Jun 5 at 14:03
  • Thanks. I learned I lot from a single answer. – Shibli Jun 5 at 14:05

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.