我希望新的引用/引文环境能够检测文本有多少行,然后增加或减少引用长度/边距。
例如:
\newenvironment{citacao}[1]%
{\list{}
% if a quotation has less than 3 lines, it will be set to 1.cm
\ifthenelse{\equal{\citacao}{true}}
{%
\setlength{\citacao}{1.8cm}
}
% if a quotation has more than 3 lines, it will be set to 4cm
{% else
\setlength{\citacao}{4cm}
}
}%
{\endlist}
如果新环境检测到引文超过 3 行,则会将文本设置为 4cm,如果没有,则将文本设置为 1.8cm。类似于:
\newenvironment{citacao}[1]%
{\list{}{\leftmargin=1.8cm\rightmargin=1.8cm}\item[]}%
{\endlist}
\newenvironment{citacao}[1]%
{\list{}{\leftmargin=4cm\rightmargin=4cm}\item[]}%
{\endlist}
我还想添加用于连字符的语言选择,例如:
\newenvironment{citacao}[1][english]%
{\list{}{\leftmargin=1.8cm\rightmargin=1.8cm}\item[]}%
{\endlist}
我尝试编写如下代码:
\newenvironment{citacao}[2]%
{\selectotherlanguage[#1]\list{}
% if a quotation has less than 3 lines, it will be set to 1.cm
\ifthenelse{\equal{\citacao}{true}}
{%
\setlength{\citacao}{1.8cm}
}
% if a quotation has more than 3 lines, it will be set to 4cm
{% else
\setlength{\citacao}{4cm}
}
}%
{\endlist}
请注意,我使用 XeLaTeX 和包csquotes
。
答案1
如果您的引文是单段,那么这样做是可行的:方法是排版具有宽边距的段落,并使用\prevgraf
来了解行数。如果行数大于 3,则调用 时会减少边距\list
。
相同的想法可以用于多段引用,但会有一些复杂之处。
\documentclass{article}
\usepackage[latin,english]{babel}
\usepackage{fontspec}
\usepackage{environ}
\newlength{\citacaoindent}
\setlength{\citacaoindent}{4cm}
\NewEnviron{citacao}[1][english]{%
\csname otherlanguage*\endcsname{#1}%
\setbox0=\vbox{
\hsize=\dimexpr\hsize-2\citacaoindent\relax
\BODY\par\expandafter
}\expandafter\ifnum\the\prevgraf>3 \setlength{\citacaoindent}{1.8cm}\fi
\list{}{\leftmargin=\citacaoindent \rightmargin=\citacaoindent}
\item[]\BODY
\endlist
}
\begin{document}
\begin{citacao}
This is a short quotation in \languagename,
typeset with a wide margin.
\end{citacao}
\begin{citacao}
This is a long quotation in \languagename, that should be
typeset with a narrower margin
because it fills more than three lines
because it fills more than three lines
because it fills more than three lines
because it fills more than three lines.
\end{citacao}
\begin{citacao}[latin]
This is a short quotation in \languagename,
typeset with a wide margin.
\end{citacao}
\begin{citacao}[latin]
This is a long quotation in \languagename, that should be
typeset with a narrower margin
because it fills more than three lines
because it fills more than three lines
because it fills more than three lines
because it fills more than three lines.
\end{citacao}
\end{document}