在小页面内居中

在小页面内居中
\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在各个点内,但所有点的规则也都发生了移动。)

答案1

我认为最简单的解决方案是添加一个额外的组来限制你在环境主体中编写的任何声明的效果

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

在此处输入图片描述

相关内容