Centralizando dentro da minipágina

Oct 07 2020
\documentclass{article}
\makeatletter
\newenvironment{myquote}[1]{%
  % to change text size, say to small
  % add \small before \par in the next line
  \par\addvspace{2ex}
  \if@nobreak
    % we're at the start of a section
    % add the indent
    \if@afterindent\else\hspace*{\parindent}\fi
    % and instruct LaTeX to reset \@afterheading at the end
    \def\reset@nobreakatend{\@afterheading}%
  \else
    \def\reset@nobreakatend{}%
  \fi
  \begin{minipage}{\dimexpr\linewidth -2\parindent\relax}%
    \def\myquoteauthorname{#1}%
  }{%
    \par\vspace{0.2ex}
    \noindent
    \hspace*{0.25\linewidth}%
      \rule{0.5\linewidth }{.4pt}
    \par\addvspace{1ex}
    \centering
    \textbf{\myquoteauthorname}\par\vspace{1ex}
  \end{minipage}
  \par\nobreak\reset@nobreakatend}
\makeatother


\begin{document}
\begin{myquote}{Diane Ravitch}
The person who knows ``how'' will always have a job. The person who knows ``why'' will always be his boss.
\end{myquote}


\begin{myquote}{Diane Ravitch}
\centering
The person who knows ``how'' will always have a job. The person who knows ``why'' will always be his boss.
\end{myquote}

\begin{myquote}{Diane Ravitch}
\begin{center}
    The person who knows ``how'' will always have a job. The person who knows ``why'' will always be his boss.
\end{center}
\end{myquote}
\end{document}

No código acima (resposta original de @egreg) , quero centralizar o texto dentro do minipage. Se eu usar \centeringa regra se move; se eu usar \begin{center} \end{center}, obtenho espaço vertical.

  1. Como posso centralizar o texto sem mover a regra e sem espaço vertical extra?
  2. Como posso ter uma versão alternativa de myquoteonde o texto é centralizado? (Tentei colocar \centeringdentro em vários pontos, mas em todos eles a regra também mudou.)

Respostas

2 campa Oct 07 2020 at 15:00

Eu diria que a solução mais fácil é adicionar um grupo adicional que restringe o efeito de qualquer declaração que você escrever no corpo do ambiente

\documentclass{article}
\makeatletter
\newenvironment{myquote}[1]{%
  % to change text size, say to small
  % add \small before \par in the next line
  \par\addvspace{2ex}
  \if@nobreak
    % we're at the start of a section
    % add the indent
    \if@afterindent\else\hspace*{\parindent}\fi
    % and instruct LaTeX to reset \@afterheading at the end
    \def\reset@nobreakatend{\@afterheading}%
  \else
    \def\reset@nobreakatend{}%
  \fi
  \begin{minipage}{\dimexpr\linewidth -2\parindent\relax}%
    \def\myquoteauthorname{#1}%
    \begingroup   % <-- ADD THIS
  }{%
    \par\endgroup\vspace{0.2ex}  % <-- ADD \endgroup HERE
    \noindent
    \hspace*{0.25\linewidth}%
      \rule{0.5\linewidth }{.4pt}
    \par\addvspace{1ex}
    \centering
    \textbf{\myquoteauthorname}\par\vspace{1ex}
  \end{minipage}
  \par\nobreak\reset@nobreakatend}
\makeatother


\begin{document}

\begin{myquote}{Diane Ravitch}
\centering
The person who knows ``how'' will always have a job. The person who knows ``why'' will always be his boss.
\end{myquote}

\begin{myquote}{Diane Ravitch}
\itshape
The person who knows ``how'' will always have a job. The person who knows ``why'' will always be his boss.
\end{myquote}

\end{document}