I have an issue with the \input
command.
As it is stated here, "it's equivalent to typing all the commands from filename.tex right into the current file where the \input line is."
I have the feeling that this is not exactly the case, at least in some situations, and I would like some clarifications.
Consider this MVCE (say, in a file main.tex
):
\documentclass{article}
\usepackage{mypackage}
\begin{document}
\begin{myenvir}{Hello}
\mycommand{World}
\end{myenvir}
\end{document}
Now the mypackage.sty
file (stored in same folder):
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mypackage}[version 0.1]
\newenvironment{myenvir}[1]
{%
\newcommand{\mycommand}[1]
{ test:command arg=##1 environment arg=#1}
\begin{center}
}
{\end{center}}
\endinput
When compiling main.tex
, this works fine, and produces.
test:command arg=World environment arg=Hello
But now, lets say for some reason (*), I want to store the command definition in a separate file. So I just change the sty file to:
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mypackage}[version 0.1]
\newenvironment{myenvir}[1]
{%
\input{fcommand}
\begin{center}
}
{\end{center}}
\endinput
And store into a file fcommand.tex
these two lines:
\newcommand{\mycommand}[1]
{ test:command arg=##1 environment arg=#1}
Shouldn't that give strictly the same result?
But when compiling main.tex
I get this error:
You can't use `macro parameter character # in horizontal mode
and the output (pdf) file holds:
test:command arg=1 environment arg=World
Question: what is going on ? A far as I understand, there seems to be a problem with argument substitution when code is in a separate file. Thus my assumption that \input
is not truly equivalent to having the code in same file.
I have also checked this related question, where the answer states to add \unskip
. So I tried this:
\input{fcommand}\unskip%
and changed the command file content:
\newcommand{\mycommand}[1]
{ test:command arg=##1 environment arg=#1}\endinput
But still got the same error.
Other related question: https://tex.stackexchange.com/a/319512/11083
(*) On the reason to try this, this question is actually a follow up on another question I am currently dealing with.
file1.tex
that looks like this, andfile2.tex
that looks like this, and I use them inmain.tex
in this way, try to provide a single, contained copy-and-paste example. I know it may not always be possible.\mycommandA
knows nothing about. Is your main question that you want to use the argument of the environment (if there was one) in a command that you define inside the environment?\input
there. It is interpreted at the point the definition is used, and there it does give the same error as if you have used the##
forms inline.\input
when the command is used. Not at the point of definition, and if you use##
at that point then you get the same error, so i would say that they are equivalent. Nothing special about input here, you can have any command\foobar
in the definition and it does not even need to be defined,\foobar
will only give an error if you use the environment and it is not defined at that point.