\footnotesize in output stream generates unexpected “Undefined control sequence”

I am writing styled text to an external file (using the newfile package). I can successfully style the text with many attributes, e.g., \large\mdseries\itshape. But, very puzzling to me, when I style the text with \footnotesize, I get an unexpected "Undefined control sequence" error, and I can't tell why it is arising:

(/compile/output.nameofstream
! Undefined control sequence.
l.1 ... {10}{12}\selectfont \abovedisplayskip 10\p
@ plus2\p@ minus5\p@ \abov...


The following MWE compiles fine and generates appropriately styled text both in the external file and in the output. The external file contains:

\relax \fontsize {14.4}{18}\selectfont \mdseries \itshape Text to be styled


But when I uncomment out \renewcommand{\styleForText}{\footnotesize}, I get the above error message and the external file contains:

\relax \fontsize {10}{12}\selectfont \abovedisplayskip 10\p@ plus2\p@ minus5\p@ \abovedisplayshortskip \z@ plus3\p@ \belowdisplayshortskip 6\p@ plus3\p@ minus3\p@ \def \leftmargin \leftmargini \parsep 5\p@ plus2.5\p@ minus\p@ \topsep 10\p@ plus4\p@ minus6\p@ \itemsep 5\p@ plus2.5\p@ minus\p@ {\leftmargin \leftmargini \topsep 6\p@ plus2\p@ minus2\p@ \parsep 3\p@ plus2\p@ minus\p@ \itemsep \parsep }\belowdisplayskip \abovedisplayskip Text to be styled


I never expected that \footnotesize would result in such different behavior than \large. What's going on?

\documentclass[12pt]{article}
\usepackage{newfile}
\newcommand{\streamName}{nameofstream}
\newcommand{\sendStyledTextToStream}[1]{%
}
\newcommand{\printStream}{%
\closeoutputstream{\streamName}%
\input{\jobname.\streamName}%
}
\newcommand{\styleForText}{\large\mdseries\itshape}
%\renewcommand{\styleForText}{\footnotesize}
\begin{document}
\newoutputstream{\streamName}
\openoutputfile{\jobname.\streamName}{\streamName}
\sendStyledTextToStream{Text to be styled}
\printStream
\end{document}

• Try with \protect\footnotesize. – GuM Nov 10 '18 at 18:23
• That does seem to work. Why is that necessary for \footnotesize when it's not required here for \large? – Jim Ratliff Nov 10 '18 at 18:25
• It’s best to \protect the \large declaration too; it doesn’t exhibit the same problem, in this particular situation, just because its expansion doesn’t happen to contain “internal” commands using the @ character, contrary to \footnotesize. On the other hand, you might have uncovered a bug: several years ago, the commands for changing the font style (e.g., \itshape, \mdseries…) were made robust so that they no longer required to be \protected; but apparently, they forgot to apply a similar change to the commands that change the font size! – GuM Nov 10 '18 at 18:36
• If you turn this into an answer, I will accept it. – Jim Ratliff Nov 10 '18 at 18:37

His solution: Use \protect\footnotesize.
It’s best to \protect the \large declaration too; it doesn’t exhibit the same problem, in this particular situation, just because its expansion doesn’t happen to contain “internal” commands using the @ character, contrary to \footnotesize. On the other hand, you might have uncovered a bug: several years ago, the commands for changing the font style (e.g., \itshape, \mdseries…) were made robust so that they no longer required to be \protected; but apparently, they forgot to apply a similar change to the commands that change the font size!