How to resolve clashing definitions of \comment in comment.sty and changes.sty without affecting either's interface?
You can via \let
save both the \comment
-macro which underlies the comment
-environment of the comment package and the \comment
-macro which comes with the changes-package.
Then you can define your own \comment
-macro which "looks" at the definition of \@currenvir
.
If \@currenvir
= comment, then executing \comment
imples calling the macro which underlies the comment-environment.
Otherwise executing \comment
implies calling the macro which comes from the changes-package.
Be aware that with the following example I said \includecomment{comment}
so that the content of comment-environments results in visual output.
\documentclass{article}
\makeatletter
\newcommand*\UD@CommentName{comment}%
\newcommand\UDsavedcommentenvironment{}%
\newcommand\UDsavedcommentmacro{}%
\usepackage{changes}[2019/01/26 v3.1.2]
\let\UDsavedcommentmacro=\comment
\let\comment=\relax
\usepackage{comment}%[2016/07/16 v3.8]
\includecomment{comment}%
%\excludecomment{comment}%
\let\UDsavedcommentenvironment=\comment
\let\comment=\relax
\newcommand\comment{%
\ifx\@currenvir\UD@CommentName
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\UDsavedcommentenvironment}{\UDsavedcommentmacro}%
}%
\makeatother
\begin{document}
\null
\comment{A pdf comment using `changes.sty'.}%
\begin{comment}
A code comment using `comment.sty'.
\end{comment}
\comment{Another pdf comment using `changes.sty'.}%
\begin{comment}
Another code comment using `comment.sty'.
\end{comment}
Some text.
\end{document}
Here's my solution based on @Ulrich Diez's answer, the main differences being the use of \@undefined
instead of \relax
and a simpler if-then-else statement:
\documentclass{article}
\makeatletter
\usepackage{comment}
\let\wfs@comment@comment\comment
\let\comment\@undefined
\usepackage{changes}
\let\wfs@changes@comment\comment
\let\comment\@undefined
\newcommand\comment{%
\ifthenelse{\equal{\@currenvir}{comment}}
{\wfs@comment@comment}
{\wfs@changes@comment}%
}
\makeatother
\begin{document}
\comment{pdf comment 1}
\begin{comment}
code comment 1
\end{comment}
\comment{pdf comment 2}
\begin{comment}
code comment 2
\end{comment}
\end{document}
Then I copied the preamble into my package, removed \makeatletter
and \makeatother
, and replaced \usepackage
-> \RequirePackage
.