
저는 Unix 명령줄과 관련된 튜토리얼을 위한 강의 자료(PDF)를 준비 중입니다. 나는 자동화(어떤 사람들은 게으름이라고도 함)를 위해 노력하기 때문에 \write18
(with --shell-escape
)와 verbatim
패키지를 사용하여 다음 작업을 수행할 수 있는 작은 패키지를 작성했습니다.
ls
명령에서 명령(예: )을 실행합니다.~ 동안내.tex
문서를 편집하여 내 문서에 조판하고,- 결과를 외부 파일로 리디렉션
stdout
합니다stderr
. - 내 문서에
stdout
및 조판을 위한 외부 파일을 입력합니다 .stderr
.tex
그러나 내가 이해한 바에 따르면 각 호출은 \write18
자체 쉘 세션을 열고 닫는다는 것입니다. 이는 약간의 왜곡과 코드 중복이 필요하기 때문에 불행한 일입니다. 특히,
- 한 세션 동안 정의된 모든 쉘 변수는 다음 세션에서 사용할 수 없습니다.
- 이전
cd
.\write18
\write18
아래 내 장난감 예를 참조하세요.
실행 중에 셸과 상호 작용할 수 있는 방법이 있습니까 ? 하지만 한 호출에서 다음 호출까지 pdflatex
셸 세션을 열어 두는 방법이 있습니까? \write18
아니면 내 요구에 맞는 더 나은 접근 방식이 있습니까?
\documentclass{article}
\usepackage{lipsum}
\begin{document}
First, initialise the repository.
% pretend that the following is an environment that both
% - runs commands at the CL
% - typesets them in the .tex document
\immediate\write18{%
cd $HOME/Desktop;
mkdir myrepo;
cd myrepo;
git init;
}
Let's see what git has to say...
\immediate\write18{%
# I'm back in $HOME :(
# I have to cd to $HOME/Desktop/myrepo, here, but I'd like to avoid it...
cd $HOME/Desktop/myrepo;
git status
# ...
}
\end{document}
답변1
\documentclass{article}
\begin{document}
\immediate\write18{echo pwd > /tmp/zzpipe}
\texttt{\input{result.txt}}
\immediate\write18{echo cd > /tmp/zzpipe}
\immediate\write18{echo pwd > /tmp/zzpipe}
\texttt{\input{result.txt}}
\immediate\write18{echo 'FOO=wibble' > /tmp/zzpipe}
\immediate\write18{echo 'echo FOO is $FOO' > /tmp/zzpipe}
\texttt{\input{result.txt}}
\end{document}
a) 명령을 받아들이도록 "서버"를 설정합니다. 저는 명명된 파이프를 사용합니다.
$ cd /tmp
$ mkfifo zzpipe
$ while true ; do eval `cat /tmp/zzpipe` >/tmp/result.txt ; done
그런 다음 위의 tex 파일을 실행합니다( 다른 곳에 기록되거나 /tmp
정렬됨 result.txt
). 출력은 다음과 같아야 합니다.
이것은 cygwin bash가 있는 창에 있으며 다른 명령줄도 비슷하지만 다른 인용 규칙이 필요할 수 있습니다. 보시다시피 cd
의 설정은 FOO
한 쓰기에서 다른 쓰기로 유지됩니다.
답변2
기본 bash 지원을 추가하여 pythontex
결국 20줄 미만의 코드를 사용하게 되었습니다. 이를 사용하려면 다음의 최신 버전이 필요합니다.GitHub. pythontex
실행해야 할 새로운 bash 코드가 있을 때마다 표준 3단계 컴파일을 사용해야 합니다 (LaTeX 실행, PythonTeX 스크립트 실행, LateX 실행). 실행해야 할 새 코드가 없으면 LaTeX만 단독으로 실행할 수 있습니다. 을 사용하지 않으므로 \write18
쉘 탈출이 필요하지 않습니다(코드 실행은 PythonTeX 스크립트에 의해 처리됨).
모든 것이 작동하는 것 같습니다. 그러나 버그를 발견하면 알려주셔야 합니다. 오류 줄 번호는 실행되는 코드의 번호와 올바르게 동기화되어야 합니다.
다음은 출력이 아래 표시된 샘플 문서입니다. 기본적으로 축어적이므로 (또는 이에 상응하는 ) \stdoutpythontex
형식을 지정할 필요가 없습니다 .\printpythontex
\stdoutpythontex
\documentclass{article}
\usepackage[makestderr, usefamily=bash]{pythontex}
\setpythontexfv{numbers=left, firstnumber=last}
\begin{document}
A block of bash...
\begin{bashblock}
myvar="myvar's value"
echo $myvar
\end{bashblock}
...with output:
\printpythontex[verbatim]
Another block, accessing the previous variable...
\begin{bashblock}
echo "In another LaTeX environment later on..."
echo $myvar
\end{bashblock}
...with output:
\printpythontex[verbatim]
A block with an error.
\begin{bashblock}
echo "In another LaTeX environment later on..."
echo $myvar
lsERROR
\end{bashblock}
Stdout:
\printpythontex[verbatim]
Stderr:
\stderrpythontex
\end{document}