한 줄에 들어갈 경우에만 인용문을 가운데에 두십시오.

한 줄에 들어갈 경우에만 인용문을 가운데에 두십시오.

견적 환경을 위한 코드를 작성했습니다. 한 줄에 들어갈 만큼 짧은 경우에만 인용문을 중앙에 맞추고 싶습니다. 그렇지 않으면 중앙에 정렬하면 안 됩니다. 이것은 코드입니다

\documentclass{article}
\usepackage{changepage, xparse}
\ExplSyntaxOn

\NewDocumentEnvironment{ myquote }{ o }{
  \begin{center}
  \begin{adjustwidth}{50pt}{50pt}
  \begin{itshape}
}{
  \end{itshape}
  \end{adjustwidth}
  \end{center}  
  \IfValueT{ #1 }{
    \vspace{-0.15cm}
    \begin{adjustwidth}{0pt}{20pt}
    \flushright { \small -\hspace{2pt}#1\hspace{2pt}- }
    \end{adjustwidth}
    \vspace{0.3cm}
  }
}

\ExplSyntaxOff

\begin{document}
\begin{myquote}[Someone]
This should be centered.
\end{myquote}

\begin{myquote}[Someone else]
This is a longer quote and therefore shouldn't be centered. This is some more text.
\end{myquote}
\end{document}

여기에 이미지 설명을 입력하세요

어떻게 해야 합니까?

답변1

, 를 사용 하고 내부의 \NewEnviron이탤릭체 너비를 확인합니다 . 보다 작으면 앞에 an 이 추가됩니다 .\BODY\hbox\linewidth\hfil\BODY

\documentclass{article}
\usepackage{changepage, environ}
\usepackage[pass,showframe]{geometry}

\NewEnviron{myquote}[1][\relax]{
  \begin{center}
  \begin{adjustwidth}{50pt}{50pt}
  \setbox0=\hbox{\itshape\BODY}%
  \begin{itshape}
  \ifdim\wd0>\linewidth\relax\BODY\else\hfil\BODY\fi
  \end{itshape}
  \end{adjustwidth}
  \end{center}  
  \ifx\relax#1\relax\else
    \vspace{-0.15cm}
    \begin{adjustwidth}{0pt}{20pt}
    \flushright { \small -\hspace{2pt}#1\hspace{2pt}- }
    \end{adjustwidth}
    \vspace{0.3cm}
  \fi
}


\begin{document}
\begin{myquote}[Someone]
This should be centered.
\end{myquote}

\begin{myquote}[Someone else]
This is a bit longer, but should still be centered.
\end{myquote}

\begin{myquote}[Someone else]
This is a longer quote and therefore shouldn't be centered. This is some more text.
\end{myquote}

\end{document}

여기에 이미지 설명을 입력하세요

관련 정보