라텍스 ifFileExist

라텍스 ifFileExist
\subsection{Checksum: Factor, Specific, and Total Variance}
Perhaps the most basic test of a regression model is if the factor and specific variances sum to equal the total variance.  Here, we 
present this diagnostic as an xy-plot, with the sum of factor and specific variance on the x-axis and the total variance on the y-axis.  We 
expect the plotted points to line up tightly along the identity line.

\clearpage
\begin{figure}[!h]

\centering

\includegraphics{\Sexpr{image_dir}xyplotFacSpecTotVarTest.pdf}

\caption{Total Variance vs. Sum of Factor and Specific Variance, All Instruments Passing Exposure Model.}

\end{figure}

질문:

"xyplotFacSpecTotVarTest.pdf"가 존재하지 않는 상황을 어떻게 처리합니까? 테이블의 내용에서 생성된 파일로, 데이터가 있을 수도 있고 없을 수도 있습니다. "xyplotFacSpecTotVarTest.pdf"가 누락된 경우 최종 PDF 생성 프로세스 오류가 발생합니다. 이 시나리오를 어떻게 처리할 수 있습니까? /IfFileExist를 시도했지만 작동하지 않는 것 같습니다. 다음 구현을 참조하세요.

\IfFileExists{{image_dir}{xyplotFacSpecTotVarTest.pdf}} { 

\clearpage

\begin{figure}[!h]

\centering

\includegraphics{\Sexpr{image_dir}{xyplotFacSpecTotVarTest.pdf}}

\typeout{File Exist: xyplotFacSpecTotVarTest.pdf}

\caption{Total Variance vs. Sum of Factor and Specific Variance, All Instruments Passing Exposure Model.}

\end{figure} } 

{\typeout{File DOESNT Exist: xyplotFacSpecTotVarTest.pdf}}

답변1

태그를 보면 knitr. 파일이 존재하는지 R 자체가 (자동으로) 결정하도록 놔두지 않겠습니까?

<<chunkname,results='asis',echo=FALSE>>=
if (file.exists(file.path(image_dir, "xyplotFacSpecTotVarTest.pdf"))) {
   cat("\\clearpage\n")
   cat("\\begin{figure}[!h]\n")
   # and so on
}
else {
  cat("\\textbf{File does not exist.}\n\n")
}
@

청크 옵션 에 유의하세요 results='asis'. 각 'd 문자열은 그대로 파일 cat에 출력됩니다 (따라서 옵션 이름). .tex즉, 각 cat'd 문자열은 컴파일할 LaTeX 명령이 됩니다.

멋진 R 함수(예: )를 생성하여 위 코드를 일반화 include_if_exists(filename)하고 여러 청크에서 재사용할 수도 있습니다.

관련 정보