0

I would like to understand how to use the expl3 command \msg_expandable_error (as used here) with \IfFileExists to get an expandable error message if some external file does not exist.

I deliberately need to use \msg_expandable_error since my editor texstudio recognizes it and prints the error message in a red font.

Furthermore, is there a latex2e counterpart for \msg_expandable_error?


Update

The desired output of red-styled error message can be obtained by the last code in @egerg's answer, when checking the log file inside texstudio while using \msg_expandable_error.

enter image description here

Diaa
  • 9,599
  • Are you sure TeXstudio recognizes the message because it expandable and not because of something else?... – gusbrs Sep 20 '21 at 20:00
  • @gusbrs I made experiments before using the cited answer, and I found that the expandable versions of warning and error messages are styled differently by texstudio. – Diaa Sep 20 '21 at 20:02
  • I really don't know TeXstudio well enough to say, but I'd be much surprised if this has anything to do with the expandability of the message function. Most likely, it parses the log based on some regular expressions like everybody else. In your experiments, did you try to use the exact same text, but with a regular message? – gusbrs Sep 20 '21 at 20:04
  • Besides, considering \file_if_exist:nTF is not expandable itself, I'd bet \IfFileExists is also not... – gusbrs Sep 20 '21 at 20:08
  • @gusbrs Yes, I am sure that the regular message command is printed normally without styled in a red font. I tried it myself, and you can check the discussion here https://chat.stackexchange.com/rooms/117475/discussion-between-phelype-oleinik-and-diaa if you are interested. – Diaa Sep 20 '21 at 20:11
  • @DavidCarlisle I haven't used any latex2e form yet. That's why I asked for an example. – Diaa Sep 20 '21 at 20:15
  • it really makes no sense to use an expandable error form for a non expandable command. the chat you link to doesn't seem related to this question??? – David Carlisle Sep 20 '21 at 20:19
  • @DavidCarlisle I found out there that the expandable one gets me the desired output. I don't know why but that's what I came up with. – Diaa Sep 20 '21 at 20:22
  • well if that's what you want to do (although obscuring your code just because of a quirk in one editor's log parsing is a bit strange) then what is the question. If you have already tested the expandable error then you must have an example of its use already? – David Carlisle Sep 20 '21 at 20:24
  • @DavidCarlisle The code in the cited answer there is complicated for me to digest, so I need a simplified example to check the existence of a file. Secondly, I would like to know if there is a similar approach with latex2e. – Diaa Sep 20 '21 at 20:30
  • the 2e form is \PackageError}{my package}{missing file}{some help} if you made an example using that and said what it is doing wrong perhaps it would be possible to answer your question, otherwise it is hard to guess – David Carlisle Sep 20 '21 at 20:33
  • 1
    This is clearly a bug in TeXStudio: TeX issues an error and TeXStudio just reports a warning. Misusing expl3 for “fixing” a bug of the front-end doesn't seem the better way to go. The same experiment on TeXmaker correctly reports error. – egreg Sep 20 '21 at 21:48

1 Answers1

2

I made the following test file

\documentclass{article}

\newcommand{\CheckIfFileExists}[1]{% \IfFileExists{#1}{OK}{\PackageError{diaa}{File #1 does not exist}{}}% }

\begin{document}

\CheckIfFileExists{plain}

\CheckIfFileExists{xuaysyeuersss}

\end{document}

If I compile it with TeXmaker, I get

enter image description here

If I compile it with TeXStudio, I get

enter image description here

The conclusion is that we're in presence of a bug in TeXStudio whereby an error message

! Package diaa Error: File xuaysyeuersss does not exist.

See the diaa package documentation for explanation. Type H <return> for immediate help. ...

l.11 \CheckIfFileExists{xuaysyeuersss}

is parsed by TeXStudio as a warning.

You can misuse expl3 for getting an error message with

\documentclass{article}

\ExplSyntaxOn \newcommand{\CheckIfFileExists}[1] { \file_if_exist:nTF { #1 } {OK} {\msg_expandable_error:nnn { diaa } { not~exist } { #1 }} } \msg_new:nnnn { diaa } { not~exist } { The~file~#1~does~not~exist } {} \ExplSyntaxOff

\begin{document}

\CheckIfFileExists{plain}

\CheckIfFileExists{xuaysyeuersss}

\end{document}

enter image description here

The main issue is that the error message is pretty uninformative. It's much better to ask the maintainers of TeXStudio to be more careful with their log file parsing.

egreg
  • 1,121,712
  • Considering your last code, if you click on the Log File tab of texstudio, you will get the output I need as shown here https://i.ibb.co/F0kds4T/image.png by styling the error message in red. Here is the output when using \msg_error https://i.ibb.co/9TSHJyD/image.png. – Diaa Sep 20 '21 at 22:14