「\\」を含む\NewEnvironの内容を1行ずつ読み取ります。

「\\」を含む\NewEnvironの内容を1行ずつ読み取ります。

新しい環境を作成し、それをカスタマイズされた配列として使用しようとしています (指定されたコードに多くの変更が加えられています)。 そのため、 の を読み取り\BODY\NewEnviron行ごとに読み取る必要があります。 行を で終了するか\\、空行で終了するかは気にしません。 ただし、1 つずつ読み取る必要があり、環境を使用する前に行数がわかりません。

私はこれを試しました:

\documentclass{article}
\usepackage{environ}
\usepackage{xstring}

\makeatletter
\long\def\findLength#1{\StrLen{#1}[\temp] The length of ``#1'' is \temp\\}
\NewEnviron{Graph}{\findLength{\BODY}}{}
\makeatother




\begin{document}

  \begin{Graph}
     test line 1 

     test line 2
  \end{Graph}

\end{document} 

ただし、と を1 行ずつではなくすべて使用します\BODY。また、改行を追加すると、 は文字列ではなくなります (改行が含まれ、エラーが発生します)。#1\StrLen

私はついに、@Bruno Le Flochのコードを使用して、新しい環境で最初の行を取得することができました。ここしかし、私はそのコードを本当に理解していませんし、それを理解してすべての引数を取るために変更するには何を読めばいいのかもわかりません。また、配列の行が終了する場所を示すために行の末尾に \ を追加することもまだできません... これが現在のコードです:

\documentclass{article}
\usepackage{environ}% defines `NewEnviron`
\usepackage{xstring}

\makeatletter
\newcommand*{\newlinecommand}[2]{%
  \newcommand*{#1}{%
    \begingroup%
    \escapechar=`\\%
    \catcode\endlinechar=\active%
    \csname\string#1\endcsname%
  }%
  \begingroup%
  \escapechar=`\\%
  \lccode`\~=\endlinechar%
  \lowercase{%
    \expandafter\endgroup
    \expandafter\def\csname\string#1\endcsname##1~%
  }{\endgroup#2\par\space}%
}
\makeatother

\makeatletter
\newlinecommand{\findLengthOfRow}{\StrLen{#1}[\temp] The length of ``#1'' is \temp}
\makeatother


\makeatletter
\long\def\findLength#1{\findLengthOfRow{#1}}
\newenvironment{Graph}{\findLength}{}
\makeatother




\begin{document}

  \begin{Graph}
     test line 1  
     test line 2    
     test line 3 
  \end{Graph}

\end{document}

どのような助けでもありがたいです。私が望む結果は、環境内の行を引数として受け取ることです。(配列がいくつの行に含まれるかはわかりません。そのため、新しい環境では具体的な数の引数を指定しないでください)。

答え1

これは、区切り文字を使用してlistofitemsを解析するための強力なパッケージを使用します。\BODY\\

\documentclass[12pt]{article}
\usepackage{listofitems,environ}
\NewEnviron{linebyline}{%
  \setsepchar{\\}%
  \readlist*\mylines{\BODY}%
  \foreachitem\x\in\mylines{Line \xcnt: \x\par}%
  The total number of lines is \textbf{\listlen\mylines[]}.
}
\begin{document}
\begin{linebyline}
This is a test\\
of whether\\
I can identify line by line.
\end{linebyline}
\end{document}

ここに画像の説明を入力してください

これは、スペースを第 2 層の区切り文字として使用して、各行を単語ごとにさらにサブ解析する方法を示しています。

\documentclass[12pt]{article}
\usepackage{listofitems,environ}
\NewEnviron{linebyline}{%
  \setsepchar{\\/ }%
  \readlist*\mylines{\BODY}%
  \foreachitem\x\in\mylines{Line \xcnt: \x{} (has \listlen\mylines[\xcnt] words,
    first/last: ``\mylines[\xcnt,1]/\mylines[\xcnt,-1]'')\par}%
  The total number of lines is \textbf{\listlen\mylines[]}.
}
\begin{document}
\begin{linebyline}
This is a test\\
of whether\\
I can identify line by line.
\end{linebyline}
\end{document}

ここに画像の説明を入力してください

答え2

行が で終わるかどうかを気にしないのであれば\\( を操作するより安全です\endlinechar)、 を使うのは本当に簡単ですexpl3

\documentclass{article}
\usepackage{xparse,environ}

\ExplSyntaxOn
\NewEnviron{Graph}
 {
  % split the environment's contents into items at the \\ separator
  \seq_set_split:NnV \l_koleygr_graph_lines_seq { \\ } \BODY
  \begin{enumerate}
  % map over the sequence, passing each item to the internal function
  \seq_map_function:NN \l_koleygr_graph_lines_seq \koleygr_graph_doline:n
  \end{enumerate}
 }

\cs_new_protected:Nn \koleygr_graph_doline:n
 {
  \tl_if_empty:nF { #1 }
   {
    \item #1
   }
 }
\ExplSyntaxOff

\begin{document}

Something at the top
\begin{Graph}
Line 1\\
Line 2\\
Line 3
\end{Graph}
Something in between
\begin{Graph}
Line 1\\
Line 2\\
Line 3\\
\end{Graph}
Something at the bottom

\end{document}

例として、各行で実行されるダミーアクションを使用しました。最後の行が で終わる場合、空かどうかのチェックが役立つことが示されています\\。他の可能性もあります。 から取得したシーケンスの最後の項目が空であるかどうかを確認するだけです\BODY。すべては、環境のコンテンツで何をしたいかによって決まります。

ここに画像の説明を入力してください

関連情報