For my own needs, I am writing a package holding some environments definitions. I also have some commands that I want to be available only inside these environments. So I define these commands inside the environment definition.
My .sty files looks like this
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mypackage}[version 0.1]
\newenvironment{myenvirA}[1]
{
\newcommand{\mycommandA}[1]
{ %
%... do something
}%
% here, the "before" part of code
}
{
% here, the "after" part of code
}
\newenvironment{myenvirB}[1]
{
\newcommand{\mycommandB}[1]
{ %
%.. do something
}%
}
{
}
Now, say I want to make \mycommandA
available inside myenvirB
blocs. Sure, I can copy/paste, but that is bad programming (error prone).
Question: Does Latex provide a way to handle this case ?
If not, can I put that command definition in a file and \input
it? (so I only have to define it once). Will that work once the package is installed in my Latex tree ?
Sorry if unclear, please point me on other question if this appears as a dupe. Couldn't find any, although this one is possibly related (?). But it is unclear to me if the answers apply to my question.
Edit forgot some information: the command has to have access to the environment arguments. Thus the idea given by Phelype Oleinik can't work (unless some trick I am not aware of ?).
Here is an MVCE:
main.tex:
\documentclass{article}
\usepackage{mypackage}
\begin{document}
\begin{envirA}{4}
\mycommandA{3}
\end{envirA}
\begin{envirB}{4}
\mycommandB{3}
\end{envirB}
\end{document}
mypackage.sty:
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mypackage}[version 0.1]
\RequirePackage{tikz}
\newenvironment{envirA}[1]
{
\newcommand{\mycommandA}[1]
{
\draw (1,0) -- (##1,#1);
}
\begin{tikzpicture}
\draw (0,0) -- (#1,1);
}
{
\end{tikzpicture}
}
\newenvironment{envirB}[1]
{
\newcommand{\mycommandB}[1]
{
\draw (0,1) -- (##1,#1);
}
\begin{tikzpicture}
\draw (0,0) -- (#1,1);
}
{
\end{tikzpicture}
}
\endinput
\newenvironment
with a private name, say,\my@internal@command
, and in the environment you can do\let\mycommand\my@internal@command
.##1
syntax). And then this fails (I'll edit question to clarify this).