五线城市的全新环境

五线城市的全新环境

我对五行代码(或 n 行代码)的新环境感兴趣。我的代码是

\documentclass[letterpaper,12pt]{article}
\usepackage{fancyhdr}
\usepackage{footnpag}
\usepackage{fontspec}
\usepackage{polyglossia}
\usepackage[total={6.5in,8.75in},top=1.2in,left=1.1in]{geometry}
\pagestyle{fancy}
\setlength{\parindent}{0pt}
\renewcommand{\headrulewidth}{1.5pt}
\renewcommand{\footrulewidth}{1.5pt}

\newcommand{\engfont}{Noto Serif Regular}
\newcommand{\eng}[1][1.4]{
           \catcode`\^=12
            \catcode`\~=12
            \fontspec[Script=Latin,Mapping=tex-text,Scale=#1]{\engfont}}

\newenvironment{fourliner}[1]{\begingroup\setlength{\leftskip}{#1}
\parshape=4 0mm \hsize 8mm \hsize 0mm \hsize 8mm \hsize }
{\par\vspace{0.4cm}\par\endgroup}

\newenvironment{fiveliner}[1]{\begingroup\setlength{\leftskip}{#1}
\parshape=5 0mm \hsize 8mm \hsize 0mm \hsize 8mm \hsize 92mm \hsize}
{\par\vspace{0.4cm}\par\endgroup}

\begin{document}
\begin{fiveliner}{0.0in}
{\eng Twinkle, twinkle, little star\\
How I wonder what you are\\
Up above the world so high\\
Like a diamond in the sky\\
Like a diamond in the sky\par}
\end{fiveliner}
\end{document}

我的输出是

在此处输入图片描述

从输出中可以看出,我为第五行插入了 92mm,希望第五行能向右对齐。我可以在新环境中添加一些东西,以便无论文本是什么,我都可以自动向右对齐,这样第五行的末尾就能与右边距对齐。

答案1

您可以吸收环境的内容并将其拆分为\\,然后逐行排版,并在偶数行添加缩进。如果空间不足,最后一行可以分离并使用较大的缩进或右对齐排版。

\documentclass{article}
\usepackage[total={6.5in,8.75in},top=1.2in,left=1.1in,showframe]{geometry}
\usepackage{fontspec}

\newfontfamily{\engfont}{Noto Serif Regular}[
  Script=Latin,
  Ligatures=TeX,
]
\newcommand{\eng}[1][1.4]{%
  \engfont\addfontfeatures{Scale=#1}%
  \linespread{#1}\selectfont
}

\ExplSyntaxOn

\NewDocumentEnvironment{nliner}{omb}
 {
  \seq_set_split:Nnn \l_tmpa_seq { \\ } { #3 }
  \seq_pop_right:NN \l_tmpa_seq \l_tmpa_tl
  \par\addvspace{0.4cm}
  \setlength{\parindent}{0pt}
  \setlength{\leftskip}{#2}
  \IfNoValueTF{#1}{\eng}{\eng[#1]}
  \seq_indexed_map_inline:Nn \l_tmpa_seq
   {
    \int_if_odd:nF { ##1 } { \hspace*{8mm} } ##2 \\
   }
  \hbox_set:Nn \l_tmpa_box { \tl_use:N \l_tmpa_tl }
  \dim_compare:nTF { \box_wd:N \l_tmpa_box + 92mm > \columnwidth }
   {% not enough space, just fill
    \hspace*{\fill}
   }
   {% enough space
    \hspace*{92mm}
   }
  \tl_use:N \l_tmpa_tl
  \par\addvspace{0.4cm}
 }{}

\ExplSyntaxOff

\begin{document}

\begin{nliner}{0.0in}
Twinkle, twinkle, little star\\
How I wonder what you are\\
Up above the world so high\\
Like a diamond in the sky\\
Like a diamond in the sky
\end{nliner}

\begin{nliner}[2]{1cm}
Twinkle, twinkle, little star\\
How I wonder what you are\\
Up above the world so high\\
Like a diamond in the sky
\end{nliner}

\end{document}

在此处输入图片描述

答案2

在此处输入图片描述

您可以进行\leftskip一些拉伸,这样最后一行(如果您设置\parfillskip为 0pt)就会向右对齐。在前面几行中,通过添加无限拉伸\\可确保\leftskip不会拉伸。

确保%在行尾添加以避免添加空格。此外,headheight太小并生成警告,但我没有在这里修复。

\documentclass[letterpaper,12pt]{article}
\usepackage{fancyhdr}
\usepackage{footnpag}
\usepackage{fontspec}
\usepackage{polyglossia}
\usepackage[total={6.5in,8.75in},top=1.2in,left=1.1in]{geometry}
\pagestyle{fancy}
\setlength{\parindent}{0pt}
\renewcommand{\headrulewidth}{1.5pt}
\renewcommand{\footrulewidth}{1.5pt}

\newcommand{\engfont}{TeX Gyre Termes}
\newcommand{\eng}[1][1.4]{%%%%%%%
           \catcode`\^=12
            \catcode`\~=12
            \fontspec[Script=Latin,Mapping=tex-text,Scale=#1]{\engfont}}

\newenvironment{fourliner}[1]{\begingroup\setlength{\leftskip}{#1}%%%%%
\parshape=4 0mm \hsize 8mm \hsize 0mm \hsize 8mm \hsize }
{\par\vspace{0.4cm}\par\endgroup}

\newenvironment{fiveliner}[1]{%%
\setlength{\leftskip}{#1 plus 1 \textwidth}%%%
\setlength{\parfillskip}{0pt}%%%
\parshape=5 0mm \hsize 8mm \hsize 0mm \hsize 8mm \hsize 0mm \hsize}
{\par\vspace{0.4cm}}

\begin{document}
\begin{fiveliner}{0.0in}
\eng Twinkle, twinkle, little star\\
How I wonder what you are\\
Up above the world so high\\
Like a diamond in the sky\\
Like a diamond in the sky
\end{fiveliner}
\end{document}

相关内容