我有下面的代码。
\documentclass{article}
\newcounter{ex}
\newenvironment{ex}{%
\par\noindent\refstepcounter{ex}{\bfseries Ex \theex.\, }
}{}
\newcommand{\Immi}[1]{\par\noindent\begin{minipage}{0.5\linewidth}
#1
\end{minipage}}
\begin{document}
\begin{ex}
Contents 1.
\end{ex}
\begin{ex}
\Immi{Contents 2 Contents 2 Contents 2Contents 2 Contents 2 Contents 2Contents 2 Contents 2 Contents 2Contents 2 Contents 2 Contents 2Contents 2 Contents 2 Contents 2Contents 2 Contents 2 Contents 2Contents 2 Contents 2 Contents 2Contents 2 Contents 2 Contents 2Contents 2 Contents 2 Contents 2Contents 2 Contents 2 Contents 2Contents 2 Contents 2 Contents 2Contents 2 Contents 2 Contents 2Contents 2 Contents 2 Contents 2Contents 2 Contents 2 Contents 2}
\end{ex}
\Immi{
\begin{ex}
Contents 3 Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3Contents 3
\end{ex}
}
\end{document}
结果显示,示例2有一个换行符。它应该像例 3。但是,命令\Immi
必须在环境主体中ex
才能由软件管理。
请帮我更正\Immi
命令或ex
环境代码。
提前谢谢您。
更新 29/4/2022。
感谢您@pascal978 先生的帮助。
我从他/她的代码中修改为以下代码。
它对我有用!
\documentclass{article}
\usepackage{lipsum}
\usepackage{xcolor}
\parindent=0pt
\newcounter{ex}
\newlength{\decal}
\newcommand{\titreexo}{\parbox[t]{\decal}{\bfseries Ex \theex.}%
}
\usepackage[a4paper]{geometry}
\newenvironment{ex}{%
\settowidth{\decal}{\bfseries Ex \theex..}
\par\refstepcounter{ex}{\titreexo}%
}{\vspace{1ex}}
\newlength{\lgtot}
\newcommand{\Immi}[2]{%
\setlength{\lgtot}{0.5\linewidth}%
\ifnum#1=1
\par
\begin{minipage}[t]{\lgtot}%
#2
\end{minipage}%
\else
%
\hspace*{-\decal}\begin{minipage}[t]{\lgtot}%
\setlength\fboxsep{0pt}
\colorbox{white}{\titreexo}#2
\end{minipage}%
\fi
}
\begin{document}
\begin{ex}%
\lipsum[12]
\end{ex}
\begin{ex}%
\Immi{0}{\lipsum[1]}
\end{ex}
\Immi{1}{%
\begin{ex}%
\lipsum[2]
\end{ex}
}
\end{document}
答案1
我不确定我理解了这个问题。我建议这样做。
\documentclass{article}
%https://tex.stackexchange.com/questions/641994/how-to-change-an-environment-in-its-body
\usepackage{lipsum}
\parindent=0pt
\newcounter{ex}
\newlength{\decal}
\setlength{\decal}{3em}%
\newcommand{\titreexo}{\parbox[t]{\decal}{\bfseries Ex \theex.}%
}
\newenvironment{ex}{%
\par\refstepcounter{ex}{\titreexo}%
}{\vspace{1ex}}
\newlength{\lgtot}
\newcommand{\Immi}[2]{%
\setlength{\lgtot}{0.5\linewidth}%
\ifnum#1=1
\addtolength{\lgtot}{\decal}%
%\addtolength{\lgtot}{3.5pt}% ????????????????
\par
\fi
%
\begin{minipage}[t]{\lgtot}%
#2
\end{minipage}%
}
\begin{document}
\begin{ex}%
Contents 1.
\end{ex}
\begin{ex}%
\Immi{0}{\lipsum[1]}
\end{ex}
\Immi{1}{%
\begin{ex}%
\lipsum[2]
\end{ex}
}
\end{document}
编辑2
您的代码的新版本。使用布尔值,命令中无需第二个参数。
\documentclass{article}
\usepackage{lipsum}
\usepackage{xcolor}
\usepackage{ifthen}% pour la définition du booléen
\parindent=0pt
\newboolean{booleenEx}
\setboolean{booleenEx}{false}
\newcounter{ex}
\newlength{\decal}
\newcommand{\titreexo}{%
\parbox[t]{\decal}{\bfseries Ex \theex.}%
}
\usepackage[a4paper]{geometry}
\newenvironment{ex}
{%
\setboolean{booleenEx}{true}
\settowidth{\decal}{\bfseries Ex \theex..}
\par\refstepcounter{ex}{\titreexo}%
}
{
\setboolean{booleenEx}{false}\vspace{1ex}
}
%\newlength{\lgtot}<-- No longer needed
\newcommand{\Immi}[1]
{%
%\setlength{\lgtot}{0.5\linewidth}%<-- No longer needed
\ifthenelse{\boolean{booleenEx}}
{%
\hspace*{-\decal}%<--- A good idea
\begin{minipage}[t]{0.5\linewidth}%
\setlength\fboxsep{0pt}
\colorbox{white}{\titreexo}#1
\end{minipage}%
}%
{%
\par
\begin{minipage}[t]{0.5\linewidth}%
#1
\end{minipage}%
}
}%
\begin{document}
\begin{ex}%
\lipsum[12]
\end{ex}
\begin{ex}%
\Immi{\lipsum[1]}
\end{ex}
\Immi{%
\begin{ex}%
\lipsum[2]
\end{ex}
}
\end{document}