在环境内排版命令,就像它们在环境之外一样

在环境内排版命令,就像它们在环境之外一样

我尝试\dialog[]{}{}在环境中使用自定义命令verse并执行它,“就好像”该命令在环境之外一样。

简而言之,我想让命令不受其所调用的环境的影响。

以下是并非如此MWE,我在其中定义了新的命令\dialogchange margin环境,这是一次粗略的尝试,试图重置边距,但没有成功。

首先,我调用\dialog命令来提供其正常输出的示例,然后在诗歌环境中调用它,最后我将歌词分成两个诗歌环境来展示所需的输出。

我想获得最后的输出,但使用第一个代码语法

\documentclass{minimal}
\usepackage{ifthen}
\usepackage{extramarks}
\usepackage{needspace}
\usepackage{verse}
%\usepackage{lineno}
%   \renewcommand\linenumberfont{\normalfont\footnotesize\sffamily}

\parindent 0pt
\parskip 0pt
\newlength{\doublespacelength}
\setlength{\doublespacelength}{1.6ex}

\newcommand{\doublespace}{\vspace{\doublespacelength}}

\newenvironment{changemargin}[2]{%
    \begin{list}{}{%
        \setlength{\partopsep}{0pt}%
        \setlength{\topsep}{0pt}%
        \setlength{\leftmargin}{#1}%
        \setlength{\rightmargin}{#2}%
        \setlength{\listparindent}{\parindent}%
        \setlength{\itemindent}{\parindent}%
        \setlength{\parsep}{\parskip}%
        }%
    \item[]}{\end{list}}

% \dialog[emotions, actions of the character]{Name}{Line}
\newcommand{\dialog}[3][]{%
  \doublespace%
  \needspace{2\baselineskip}%
  \begin{changemargin}{0in}{0in}%
        { \textsc{#2} \ifthenelse{\equal{#1}{}}{}{(\textit{#1})}%
        \par
        }%
    \end{changemargin}%
  \extramarks{}{}%
  \begin{changemargin}{0.3in}{0in} \texttt{#3} \end{changemargin}%
}


\begin{document}
% Example basic output
\dialog[excited]{Peter}{Oh my god a MWE!}

\bigskip

% Undesired output - but this is how I want to write the code
\settowidth{\versewidth}{My custom lenght}
\begin{verse}[\versewidth]
Some good\\
old poetry,\\

\dialog[excited]{Peter}{Oh my god a MWE!}

or maybe a song\\
or a ballata\\
\end{verse}

\bigskip
\bigskip


% Desired output but I had to split the lyrcs in two verse environments which is unwanted.
\begin{verse}[\versewidth]
Some good\\
old poetry,\\
\end{verse}

\dialog[excited]{Peter}{Oh my god a MWE!}

\begin{verse}[\versewidth]
or maybe a song\\
or a ballata\\
\end{verse}

\end{document}

在此处输入图片描述

答案1

不确定这还会破坏什么。我做了 3 件事:

  1. 我修改\verse\endverse定义了一个宏\verseskip,在一个组内,

  2. 我改变了\parsep内部\changemargin,并且

  3. 我做了\leftskip\verseskip两次changemargin

附言:如果我有强迫症,我会把“a MWE”改成“an MWE”。

妇女权利委员会:

\documentclass{article}
\usepackage{ifthen}
\usepackage{extramarks}
\usepackage{needspace}
\usepackage{verse}
%\usepackage{lineno}
%   \renewcommand\linenumberfont{\normalfont\footnotesize\sffamily}
\newcommand\verseskip{0pt}

%%% I CHANGED VERSE
\let\svverse\verse
\let\svendverse\endverse
\def\verse{\bgroup\def\verseskip{-1.84in}\svverse}
\def\endverse{\svendverse\egroup}
%%%

\parindent 0pt
\parskip 0pt
\newlength{\doublespacelength}
\setlength{\doublespacelength}{1.6ex}

\newcommand{\doublespace}{\vspace{\doublespacelength}}

\newenvironment{changemargin}[2]{%
    \begin{list}{}{%
        \setlength{\partopsep}{0pt}%
        \setlength{\topsep}{0pt}%
        \setlength{\leftmargin}{#1}%
        \setlength{\rightmargin}{#2}%
        \setlength{\listparindent}{\parindent}%
        \setlength{\itemindent}{\parindent}%
        \setlength{\parsep}{\parskip}%
        \setlength\parsep{-8pt}% I ADDED THIS
        }%
    \item[]\leftskip\verseskip}{\leftskip\verseskip\end{list}}% I ADDED \leftskip 2X

% \dialog[emotions, actions of the character]{Name}{Line}
\newcommand{\dialog}[3][]{%
  \doublespace%
  \needspace{2\baselineskip}%
  \begin{changemargin}{0in}{0in}%
        { \textsc{#2} \ifthenelse{\equal{#1}{}}{}{(\textit{#1})}%
        \par
        }%
    \end{changemargin}%
  \extramarks{}{}%
  \begin{changemargin}{0.3in}{0in} \texttt{#3} \end{changemargin}%
}


\begin{document}
% Example basic output
\dialog[excited]{Peter}{Oh my god a MWE!}

\bigskip

% Undesired output - but this is how I want to write the code
\settowidth{\versewidth}{My custom lenght}
\begin{verse}[\versewidth]
Some good\\
old poetry,\\

\dialog[excited]{Peter}{Oh my god a MWE!}

or maybe a song\\
or a ballata\\
\end{verse}

\bigskip
\bigskip


% Desired output but I had to split the lyrcs in two verse environments which is unwanted.
\begin{verse}[\versewidth]
Some good\\
old poetry,\\
\end{verse}

\dialog[excited]{Peter}{Oh my god a MWE!}

\begin{verse}[\versewidth]
or maybe a song\\
or a ballata\\
\end{verse}

\end{document}

在此处输入图片描述

答案2

部分解决方法是创建两个版本的 \dialog 命令,一个用于在内部执行,一个用于在 verse 环境之外执行。我不会将此标记为答案然而原因有二:

  • 我并没有“好像”在诗歌环境中执行 \dialog 命令,实际上我关闭了环境,执行了命令并重新打开了环境,在我看来这是一个不雅的操作。
  • 即使新命令 \dialog 在诗句环境内外是相同的,诗句环境“内部”的 \dialog 输出的第二行也以某种方式缩进。我现在还不知道为什么该行缩进,需要进一步检查。

以下是 MWE 的概要:

\documentclass{article}

\usepackage{ifthen}
\usepackage{verse}

\newcommand{\dialog}[3][]{%
         \textsc{#2} \ifthenelse{\equal{#1}{}}{}{(\textit{#1})}%
         \par%
\texttt{#3}%
}

\def \dout{\dialog[scioccata]{Nena}{La cioccolata fa schifo!}}
\def \din{\end{verse}\dialog[scioccata]{Nena}{La cioccolata fa schifo!}\begin{verse}[\versewidth]}

\makeatletter
    \newcommand{\currentEnv}{%
     \ifthenelse{\equal{\@currenvir}{verse}}{\din}{\dout}%
    }
\makeatother



\begin{document}

\dout

\settowidth{\versewidth}{E’ una storia triste}
\begin{verse}[\versewidth]
    trallalero\\
    \currentEnv
    trallallà
\end{verse}

\end{document}

在此处输入图片描述

相关内容