add tag
निरंजन
@samcarter suggested me in [this](https://topanswers.xyz/tex?q=993#a1189) answer to use `setspace` package for changing the `baselinestretch`, but some days ago I came to know that the `setspace` package doesn't work with `memoir` class. It has its own command `linespread` for this purpose. [This](https://tex.stackexchange.com/q/415601/174620) SE question was helpful. I tweaked the code provided by @joulev in [this](https://topanswers.xyz/tex?q=1063#a1276) answer like following.  
```  
\ProvidesPackage{test}
\RequirePackage{pgfkeys}
\RequirePackage{setspace}
\def\marathiset#1{\pgfkeys{test/.cd,#1}}
\pgfkeys{
    test/.is family,test/.cd,
    baseline/.code={\@ifclassloaded{memoir}{\linespread{#1}}{\setstretch{#1}}\selectfont},
    baseline=1.5,
    baseline/.default=1
}
\DeclareOption*{\expandafter\marathiset\expandafter{\CurrentOption}}
\providecommand{\change}[1]{\marathiset{baseline=#1}}
\ProcessOptions
```
but now the `\change` command doesn't work. Try this code -
```
\documentclass{memoir}
\usepackage{lipsum}
\usepackage{test}

\begin{document}
\change{5}
        \lipsum[1-1]
\end{document}
```

Basically I want to use `linespread` whenever `memoir` class is used and otherwise `setstretch` command by package `setspace`.
Top Answer
Skillmon
Instead of using `\@ifclassloaded` in the key's code, you could test `\AtBeginDocument` which class is used and define the key accordingly:

```
\ProvidesPackage{test}
\RequirePackage{pgfkeys}
\RequirePackage{setspace}
\def\marathiset#1{\pgfqkeys{/test}{#1}}
\newcommand*\marathi@baselineskip{1.5}
\pgfkeys
  {%
    test/.is family, test/.cd,
    baseline/.store in=\marathi@baselineskip,
    baseline/.default=1
  }
\AtBeginDocument
  {%
    \@ifclassloaded{memoir}
      {%
        \linespread{\marathi@baselineskip}\selectfont
        \pgfkeys{test/baseline/.code=\linespread{#1}\selectfont}%
      }%
      {%
        \setstretch{\marathi@baselineskip}%
        \pgfkeys{test/baseline/.code=\setstretch{#1}}%
      }%
  }
\DeclareOption*{\expandafter\marathiset\expandafter{\CurrentOption}}
\providecommand{\change}[1]{\marathiset{baseline=#1}}
\ProcessOptions\relax
```

Note that I also changed `\pgfkeys{test/.cd,#1}` to `\pgfqkeys{/test}{#1}`, which is faster.
Answer #2
samcarter
Another approach could be to test the documentclass during the package loading (caveat: this won't deal with edge cases described in [this comment](https://topanswers.xyz/transcript?room=1259&id=62326#c62326)):

```
\documentclass{memoir}
\usepackage{lipsum}


\begin{filecontents*}[overwrite]{test.sty}
\ProvidesPackage{test}
\RequirePackage{pgfkeys}
\RequirePackage{setspace}

\newif\ifmemoir
\@ifclassloaded{memoir}{
  \memoirtrue
}{}

\def\marathiset#1{\pgfkeys{test/.cd,#1}}
\pgfkeys{
    test/.is family,test/.cd,
    baseline/.code={
      \ifmemoir
        \linespread{#1}%
      \else
        \setstretch{#1}%
      \fi
      \selectfont},
    baseline=1.5,
    baseline/.default=1
}
\DeclareOption*{\expandafter\marathiset\expandafter{\CurrentOption}}
\providecommand{\change}[1]{\marathiset{baseline=#1}}
\ProcessOptions
\end{filecontents*}

\usepackage{test}

\begin{document}
\change{4}


        \lipsum[1-1]
\end{document}
```
Answer #3
frougon
Here is my take at this; I hope this helps a little bit. I renamed `\change` to `\changeBaseline`. It works both in the preamble and in the `document` environment.

```
\ProvidesPackage{test}
\RequirePackage{pgfkeys}

\newif\ifusingmemoir

\pgfkeys{
    /test/.is family, /test/.cd,
    baseline/.code={%
      \ifusingmemoir \linespread{#1}\else \setstretch{#1}\fi
      \selectfont
    },
    baseline/.default=1,
    @using memoir/.is if=usingmemoir,
    @initial baseline/.initial=1.5,
    %
    /test/package options/.is family, /test/package options/.cd,
    baseline/.forward to=/test/@initial baseline,
    baseline/.default=1,
}

\newcommand{\testpkg@process@option}[1]{\pgfkeys{/test/package options/.cd, #1}}
\DeclareOption*{\expandafter\testpkg@process@option\expandafter{\CurrentOption}}

\newcommand{\marathiset}[1]{\pgfkeys{/test/.cd, #1}}
% Will be redefined in the \AtBeginDocument hook
\newcommand{\changeBaseline}[1]{\pgfkeys{/test/@initial baseline=#1}}

\AtBeginDocument{%
  \@ifclassloaded{memoir}
    {\pgfkeys{/test/@using memoir=true}}
    {\RequirePackage{setspace}\pgfkeys{/test/@using memoir=false}}%
  \pgfkeys{/test/baseline/.expanded=\pgfkeysvalueof{/test/@initial baseline}}%
  \renewcommand{\changeBaseline}[1]{\marathiset{baseline=#1}}%
}

\ProcessOptions
```

Note: the `@using memoir/.is if=usingmemoir` is just syntactic sugar here. One could as well call `\usingmemoirtrue` and `\usingmemoirfalse` in the `\AtBeginDocument` hook. Also, for a real package, you would add a namespace prefix (e.g., `\newif\ifmypkg@usingmemoir` instead of `\newif\ifusingmemoir`).

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.