
새로운 환경을 생성하고 이를 사용자 정의된 배열로 사용하려고 합니다(주어진 코드에서 많은 변경 사항 포함). 그래서 나는 \BODY
의 내용을 \NewEnviron
한 줄씩 읽어야 합니다 . \\
빈 줄 등으로 줄을 마무리해야 하는지는 상관하지 않습니다 . 그런데 하나씩 읽어야하고 환경을 사용하기 전에는 몇 줄이 있는지 모르겠습니다.
나는 이것을 시도했다:
\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}
그러나 \BODY
한 줄씩 사용하지 않고 모두를 사용합니다. 또한 줄바꿈을 추가하면 #1
of는 \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}
다음은 공백을 두 번째 계층 구분 기호로 사용하여 각 줄을 단어별로 하위 구문 분석하는 방법을 보여줍니다.
\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
비어 있는지 간단히 확인할 수 있습니다. 그것은 모두 환경의 콘텐츠로 무엇을 하려는지에 달려 있습니다.