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.texthat looks like this, andfile2.texthat looks like this, and I use them inmain.texin this way, try to provide a single, contained copy-and-paste example. I know it may not always be possible. – Werner Nov 03 '18 at 18:02\mycommandAknows 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? – Werner Nov 03 '18 at 18:04##1and#1the way you do. – Werner Nov 03 '18 at 19:20test:command arg=##1 environment arg=#1?) Or maybe I don't understand what you question is ? What do you mean by "there are ways around using ..." ? – kebs Nov 03 '18 at 23:45\inputthere. 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. – David Carlisle Nov 04 '18 at 11:55\inputwhen 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\foobarin the definition and it does not even need to be defined,\foobarwill only give an error if you use the environment and it is not defined at that point. – David Carlisle Nov 04 '18 at 13:00\inputbut it doesn't with it? – touhami Nov 04 '18 at 13:27##in the input file to work as the OP intended. But then I'm reading it as tex reads it, presumably kebs is reading it with a different expectation... – David Carlisle Nov 04 '18 at 13:28#includein C or C++ language, but it appears it is not the same (although maybe you can also have some oddities with#include? can't be sure...) – kebs Nov 05 '18 at 10:56