미니 페이지 중앙에 배치

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}

위의 코드 (@egreg의 원래 답변) 에서 텍스트를 minipage. \centering규칙 이동을 사용 하면; 를 사용 \begin{center} \end{center}하면 수직 공간이 생깁니다.

  1. 규칙을 이동하지 않고 추가 수직 공간없이 텍스트를 중앙에 배치하려면 어떻게해야합니까?
  2. myquote텍스트가 중앙 에 위치 하는 대체 버전을 어떻게 가질 수 있습니까? ( \centering여러 지점에서 내부 를 배치하려고 했지만 모두 규칙이 이동했습니다.)

답변

2 campa Oct 07 2020 at 15:00

가장 쉬운 해결책은 환경 본문에 작성하는 선언의 효과를 제한하는 추가 그룹을 추가하는 것입니다.

\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}