This solution runs the plot twice, using the actual width of the first run to adjust the width of the second.
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usepackage{showframe}
\newsavebox{\tempbox}
\begin{document}
\savebox{\tempbox}{\begin{tikzpicture}
\begin{groupplot}[group style={group name=myplot, group size= 2 by 1, horizontal sep=2.5em},
width=0.5\textwidth,
every axis plot/.append style={line width=0.4mm, line join=round},
xmin=-1,xmax=1,
ymin=-1,ymax=1.,
]
\nextgroupplot[]
%\addplot + [mark=none, domain=-1:1] {x};
\nextgroupplot[]
%\addplot + [mark=none, domain=-1:1] {x^2};
\end{groupplot}
\end{tikzpicture}}% measure width
\noindent\begin{tikzpicture}[]
\begin{groupplot}[group style={group name=myplot, group size= 2 by 1, horizontal sep=2.5em},
width={\dimexpr \textwidth-0.5\wd\tempbox},
every axis plot/.append style={line width=0.4mm, line join=round},
xmin=-1,xmax=1,
ymin=-1,ymax=1.,
]
\nextgroupplot[]
\addplot + [mark=none, domain=-1:1] {x};
\nextgroupplot[]
\addplot + [mark=none, domain=-1:1] {x^2};
\end{groupplot}
\draw[red] (current bounding box.south west) rectangle (current bounding box.north east);
\end{tikzpicture}
\end{document}
This illustrates how one could enter the code only once using the groupwidth enviroment.
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usepackage{environ}
\usepackage{showframe}
\NewEnviron{groupwidth}[1]{% #1 = number of groupplots per row
\tikzset{/pgfplots/width={\dimexpr \textwidth/#1}}%
\sbox0{\BODY}%
\tikzset{/pgfplots/width={\dimexpr 2\textwidth/#1 - \wd0/#1}}%
\BODY}
\begin{document}
\noindent\begin{groupwidth}{2}
\begin{tikzpicture}
\begin{groupplot}[group style={group name=myplot, group size= 2 by 1, horizontal sep=2.5em},
every axis plot/.append style={line width=0.4mm, line join=round},
xmin=-1,xmax=1,
ymin=-1,ymax=1.,
]
\nextgroupplot[]
\addplot + [mark=none, domain=-1:1] {x};
\nextgroupplot[]
\addplot + [mark=none, domain=-1:1] {x^2};
\end{groupplot}
\end{tikzpicture}%
\end{groupwidth}
\end{document}
This version uses the aux file to communicate between runs. Instead of drawing the tikzpicture twice, you run the entire document at least two times.
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usepackage{showframe}
\newcommand{\newgroupwidth}[2]% #1 = id, #2 = width to use
{\expandafter\xdef\csname groupwidth#1\endcsname{#2}}
\newcounter{groupwidth}
\newsavebox{\groupwidthbox}
\makeatletter
\newenvironment{groupwidth}[1]% #1 = number of groupplots per row
{\edef\groupnumber{#1}% save for \endgroupwidth
\stepcounter{groupwidth}%
@ifundefined{groupwidth\thegroupwidth}{\pgfmathsetlengthmacro{\mywidth}{\textwidth/\groupnumber}}%
{\expandafter\let\expandafter\mywidth\csname groupwidth\thegroupwidth\endcsname}%
\begin{lrbox}{\groupwidthbox}%
\tikzset{/pgfplots/width={\mywidth}}%
\ignorespaces}%
{\end{lrbox}%
\usebox\groupwidthbox
\pgfmathsetlengthmacro{\mywidth}{\mywidth + (\textwidth - \wd\groupwidthbox)/\groupnumber}
\immediate\write@auxout{\string\newgroupwidth{\thegroupwidth}{\mywidth}}}
\makeatother
\begin{document}
\noindent\begin{groupwidth}{2}
\begin{tikzpicture}
\begin{groupplot}[group style={group name=myplot, group size= 2 by 1, horizontal sep=2.5em},
every axis plot/.append style={line width=0.4mm, line join=round},
xmin=-1,xmax=1,
ymin=-1,ymax=1.,
]
\nextgroupplot[]
\addplot + [mark=none, domain=-1:1] {x};
\nextgroupplot[]
\addplot + [mark=none, domain=-1:1] {x^2};
\end{groupplot}
%\draw[red] (current bounding box.south west) rectangle (current bounding box.north east);
\end{tikzpicture}%
\end{groupwidth}
\end{document}
.5\textwidthand check with\usepackage[showframe]{geometry}if this works in your particular case. – Markus G. Apr 29 '21 at 11:33.5\textwidthbut it's smaller than the full width. – aaragon Apr 29 '21 at 11:3511.1484cm. – aaragon Apr 29 '21 at 15:05width=number? – aaragon Apr 29 '21 at 15:07