Латекс 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" не существует. Файл, созданный на основе содержимого таблицы, которая, в свою очередь, может содержать или не содержать данные. Окончательный процесс генерации PDF-файла выдает ошибки, если отсутствует "xyplotFacSpecTotVarTest.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'опцию chunk. Каждая catстрока 'd будет выведена в .texфайл в том виде, в котором она есть (отсюда и название опции). Другими словами, каждая catстрока 'd будет командой LaTeX для компиляции.

Вы даже можете обобщить приведенный выше код, создав необычную функцию R, например include_if_exists(filename), и повторно использовать ее во многих фрагментах.

Связанный контент