自定义报价小页面命令

自定义报价小页面命令
\documentclass[12pt]{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[
    paperwidth=32pc,%
    paperheight=48pc,%
    margin=4pc,%
]{geometry}

\begin{document}

\begin{center}
\vspace{3ex}
    \begin{minipage}{0.75\textwidth}
    When asked what it was like to set about proving something, the mathematician
    likened proving a theorem to seeing the peak of a mountain and trying to climb
    to the top. One establishes a base camp and begins scaling the mountain's sheer
    face, encountering obstacles at every turn, often retracing one's steps and
    struggling every foot of the journey. Finally when the top is reached, one
    stands examining the peak, taking in the view of the surrounding countryside
    and then noting the automobile road up the other side!

    \vspace{1ex}
    \centering
     \noindent\hfil\rule{0.5\textwidth}{.4pt}\hfil

    \vspace{1ex}
    \textsc{Robert Kleinhenz}
    \end{minipage}    
\vspace{3ex}
\end{center} 

\end{document}

在此处输入图片描述

  1. 我怎样才能使宽度如此之大,以至于左右minipage都有边距。我试过了(使用包),但没有任何效果。\parindent\begin{minipage}{\textwidth-\parindent}-\parindentcalc
  2. 为什么水平线没有在里面居中minipage
  3. 我怎样才能把这个设置变成command?(所以我只是写了类似的东西\myquote{This is a quote.}{---From somebody}

答案1

您需要\dimexprminipage表达式进行求值,然后\dimexpr\textwidth-2\parindent

规则未居中,因为您\centering激活了 和,但 TeX 会丢弃行尾的\hfil\rule{}{}\hfil粘连(包括),因此最后一个被删除。并且您有和正在执行,因此左边有 2 个,右边有 1 个,因此出现错位。\hfil\hfil\centering\leftskip\rightskip\hfil\hfil

将其变成命令的最简单情况是使用\newcommand\myquote[2]。如果您想要对此 quote 环境进行其他设置,则可能需要添加可选参数和 keyval 解析器来设置一些选项。

我更改了示例的页面布局,但您可以改回来。

在此处输入图片描述

\documentclass[12pt]{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[
    a6paper,
    showframe,
]{geometry}

\newcommand\myquote[2]{%
  \par
  \begin{minipage}{\dimexpr\textwidth-2\parindent\relax}
    #1
    \par\vspace{1ex}
    \noindent
    \hspace*{0.25\textwidth}%
      \rule{0.5\textwidth}{.4pt}
    \par\vspace{1ex}
    \centering
    \textsc{#2}\par
  \end{minipage}
  \par}

\begin{document}

\myquote{
  When asked what it was like to set about proving something, the mathematician
  likened proving a theorem to seeing the peak of a mountain and trying to climb
  to the top. One establishes a base camp and begins scaling the mountain's sheer
  face, encountering obstacles at every turn, often retracing one's steps and
  struggling every foot of the journey. Finally when the top is reached, one
  stands examining the peak, taking in the view of the surrounding countryside
  and then noting the automobile road up the other side!
}{Robert Kleinhenz}

\end{document}

下面是另一种定义,使规则的宽度与引文作者的姓名相同:

\makeatletter
\newcommand\myquote[2]{%
  \par
  \begin{minipage}{\dimexpr\textwidth-2\parindent\relax}
    #1
    \par\vspace{1ex}
    \noindent
    \settowidth\dimen@{\textsc{#2}}%
    \ifdim\dimen@>\textwidth
      \dimen@\textwidth
    \fi
    \hspace*{0.5\dimexpr\textwidth-\dimen@}%
      \rule{\dimen@}{.4pt}
    \par\vspace{1ex}
    \centering
    \textsc{#2}\par
  \end{minipage}
  \par}
\makeatother

在此处输入图片描述

但是,正如 daleif 在评论中所说,命令通常更适合较短的文本。较长的文本往往会变得混乱。您需要在多行之间平衡括号,您需要担心\long\defs,逐字不再起作用,以及许多其他问题。对于大段文本,环境更自然(事实上,引用包通常使用环境)。以下是上述命令的环境版本,您都可以将它们用作:

\begin{myquote}{<author>}
  <quote>
\end{myquote}
% Fixed length rule
\newenvironment{myquote}[1]{%
  \par
  \begin{minipage}{\dimexpr\textwidth-2\parindent\relax}%
    \def\myquoteauthorname{#1}%
  }{%
    \par\vspace{1ex}
    \noindent
    \hspace*{0.25\textwidth}%
      \rule{0.5\textwidth}{.4pt}
    \par\vspace{1ex}
    \centering
    \textsc{\myquoteauthorname}\par
  \end{minipage}
  \par}
% Variable length rule
\makeatletter
\newenvironment{myquote}[1]{%
  \par
  \begin{minipage}{\dimexpr\textwidth-2\parindent\relax}%
    \def\myquoteauthorname{#1}%
  }{%
    \par\vspace{1ex}
    \noindent
    \settowidth\dimen@{\textsc{\myquoteauthorname}}%
    \ifdim\dimen@>\textwidth
      \dimen@\textwidth
    \fi
    \hspace*{0.5\dimexpr\textwidth-\dimen@}%
      \rule{\dimen@}{.4pt}
    \par\vspace{1ex}
    \centering
    \textsc{\myquoteauthorname}\par
  \end{minipage}
  \par}
\makeatother

相关内容