add tag
samcarter
If I compile the following `.dtx` file with `pdflatex test.dtx` I get a `.sty` file with a correctly escaped line ending

```
\newcommand{\foo}[2][]{%
x}
```

However in the `.pdf` created from the `.dtx` file, an additional space appears between the two `x`: 

![Screen Shot 2021-04-28 at 17.04.54.png](/image?hash=22dfc9b4ceee94e461d0145f09b54d1e2478addef34f233952823219b0fdd065)

I know that I could work around this with

```
\newcommand{\foo}[2][]{%^^A
x}
```

but this would also appear in the `.sty` file and make the code there harder to read. 

**Is there any other way to protect the line ending in the `.dtx` file without adding any unnecessary things to the `.sty` file?**

`test.dtx`:

```
% \iffalse meta-comment
%<*internal>
\def\nameofplainTeX{plain}
\ifx\fmtname\nameofplainTeX\else
  \expandafter\begingroup
\fi
%</internal>
%<*install>
\input docstrip.tex
\keepsilent
\askforoverwritefalse
\generate{
  \file{\jobname.sty}{\from{\jobname.dtx}{package}}
}
%</install>
%<install>\endbatchfile
%<*internal>
\generate{
  \file{\jobname.ins}{\from{\jobname.dtx}{install}}
}
\nopreamble\nopostamble
\ifx\fmtname\nameofplainTeX
  \expandafter\endbatchfile
\else
  \expandafter\endgroup
\fi
%</internal>

%<*driver>
\documentclass{ltxdoc} 
%\DisableImplementation
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi

\newcommand{\foo}[2][]{%
x}

% x\foo
\endinput
```
Top Answer
Phelype Oleinik
Not sure if this is the answer you want, but...

What is in the `.dtx` file is _source code_, which often is identical to _executable code_, but not necessarily.  The source is meant to be typeset by LaTeX using a class like `ltxdoc` and it is meant to be extracted by a program like Docstrip, and what is extracted then can be executed as package code.

What this means in your example is that you should load your package in the typesetting run so that you can use its commands.  Since you are using this self-extracting `.dtx` layout, the code for your package is automatically extracted before the LaTeX run, so you can just `\usepackage{test}` in the preamble, and then use it normally in the documentation (even before their definition in the `.dtx`):

> ![test.png](/image?hash=1aa7967a30c8dc1c6ab890e6559836b5fdce79b281aab6a86258eeac5321d710)

```
% \iffalse meta-comment
%<*internal>
\def\nameofplainTeX{plain}
\ifx\fmtname\nameofplainTeX\else
  \expandafter\begingroup
\fi
%</internal>
%<*install>
\input docstrip.tex
\keepsilent
\askforoverwritefalse
\generate{
  \file{\jobname.sty}{\from{\jobname.dtx}{package}}
}
%</install>
%<install>\endbatchfile
%<*internal>
\generate{
  \file{\jobname.ins}{\from{\jobname.dtx}{install}}
}
\nopreamble\nopostamble
\ifx\fmtname\nameofplainTeX
  \expandafter\endbatchfile
\else
  \expandafter\endgroup
\fi
%</internal>

%<*driver>
\documentclass{ltxdoc}
\usepackage{test} % <- load package; could also be \usepackage{\jobname}
%\DisableImplementation
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
%
% \begin{macro}{\foo}
%  Explanation what \cs{foo} does
%  and a demonstration: x\foo{}
%    \begin{macrocode}
\newcommand{\foo}[2][]{%
  x}
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%
\endinput
```

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.