4

If I do:

\message{^^J! my very long message that will be cut on many lines because LaTeX for some weird reasons like to manually cut the lines instead of letting the terminal do it^^J}

I will see something like this in the log:

! my very long message that will be cut on many lines because LaTeX for some we
ird reasons like to manually cut the lines instead of letting the terminal do i
t

But I'd like to prepend ! to all the lines, because TeXstudio will only print lines starting with a ! as errors. How could I automatically add a symbol when a new line is created in the log?

MWE

\documentclass[]{article}

\begin{document}

\message{! my very long message that will be cut on many lines because LaTeX for some weird reasons like to manually cut the lines instead of letting the terminal do it}

\end{document}

EDIT: to clarify, I'd like the log to contain:

! my very long message that will be cut on many lines because LaTeX for some we
! ird reasons like to manually cut the lines instead of letting the terminal do
! it

(or even better if it could break at word boundaries, but hey, lets not ask too much)

tobiasBora
  • 8,684
  • @StevenB.Segletes What would that change? – tobiasBora Dec 21 '23 at 22:56
  • @StevenB.Segletes yes, but I want ! to be on every line cf my edit). I'm thinking I could manually write my own \message{} that cuts the string in chunks, but there might be better solutions. – tobiasBora Dec 21 '23 at 22:59
  • Does your edit indicate you would like to add arbitrary line breaks in the middle of a \message? – Steven B. Segletes Dec 21 '23 at 22:59
  • @StevenB.Segletes no, just I'd like to add a symbol ! in front of any line in the log – tobiasBora Dec 21 '23 at 23:00
  • \message is a tex primitive you can not control its line breaking other than setting the line length to be very long so no line breaking occurs. – David Carlisle Dec 21 '23 at 23:05
  • you can of course break up the string by hand, but why use \message in that case rather than \write ? – David Carlisle Dec 21 '23 at 23:06
  • @DavidCarlisle Yeah, I’m currently doing the code to break the string by hand, seems to work ok. What do you mean with write? The goal is to write in the log file. – tobiasBora Dec 21 '23 at 23:19
  • 1
    yes the usual way is to use \write-1{kkkk} or similar (\message is a little weird as it doesn't force a linebreak per message), latex's \typeout for example is \write not \message – David Carlisle Dec 21 '23 at 23:29

3 Answers3

4

Here, I introduce \xmessage, which takes an extended message and breaks it at the first space that exceeds a set limit (here 4o characters). Each new line begins with a ! character.

Note that any given command sequence in the \xmessage only counts as a single character.

\documentclass[]{article}
\usepackage{tokcycle}
\newcounter{charcount}
\makeatletter
\newcommand\xmessage[1]{\bgroup%
  \aftertokcycle{\typeout{! \the\cytoks}}%
  \tokcycle{\addcytoks{##1}\stepcounter{charcount}}
  {\processtoks{##1}}
  {\addcytoks{##1}\stepcounter{charcount}}
  {\addcytoks{##1}\stepcounter{charcount}\splitline}{#1}%
  \egroup}
\newcommand\splitline{%
  \ifnum\value{charcount}>40\typeout{! \the\cytoks}%
  \setcounter{charcount}{0}\cytoks{}\fi}
\begin{document}
\xmessage{my very long message that will be cut on many lines because LaTeX for some weird reasons like to manually cut the lines instead of letting the terminal do it}
\end{document}

enter image description here

2

Split the input (possibly after full expansion, with the *-variant) at spaces and then prepare the message computing the total length of the line and taking the decision to break if the max_print_line limit is exceeded.

\documentclass{article}

\ExplSyntaxOn

% get the current length of error lines \sys_get_shell:nnN { kpsewhich ~ -var-value=error_line } {} \l_tmpa_tl \int_const:Nn \c__tobiasbora_prefixmessage_limit_int { \l_tmpa_tl }

\NewDocumentCommand{\prefixmessage}{sO{!}m} { \IfBooleanTF { #1 } { \tobiasbora_prefixmessage:ne { #2 } { #3 } } { \tobiasbora_prefixmessage:nn { #2 } { #3 } } }

\seq_new:N \l__tobiasbora_prefixmessage_text_seq \str_new:N \l__tobiasbora_prefixmessage_text_str \int_new:N \l__tobiasbora_prefixmessage_len_int

\cs_new_protected:Nn \tobiasbora_prefixmessage:nn { \seq_set_split:Nne \l__tobiasbora_prefixmessage_text_seq { ~ } { \tl_to_str:n { #2 } } % start up \str_set:Nn \l__tobiasbora_prefixmessage_text_str { #1 } \int_zero:N \l__tobiasbora_prefixmessage_len_int % prepare the string to output \seq_map_inline:Nn \l__tobiasbora_prefixmessage_text_seq { \int_add:Nn \l__tobiasbora_prefixmessage_len_int { \str_count:n { ##1 } + 1 } \int_compare:nNnTF { \l__tobiasbora_prefixmessage_len_int } > { \c__tobiasbora_prefixmessage_limit_int - \str_count:n { #1 } } { \str_put_right:Nn \l__tobiasbora_prefixmessage_text_str { ^^J #1 ~ ##1 } \int_set:Nn \l__tobiasbora_prefixmessage_len_int { \str_count:n { ##1 } + 1 } } { \str_put_right:Nn \l__tobiasbora_prefixmessage_text_str { ~ ##1 } } } % output \iow_term:e { ^^J \l__tobiasbora_prefixmessage_text_str ^^J } } \cs_generate_variant:Nn \tobiasbora_prefixmessage:nn { ne }

\ExplSyntaxOff

\prefixmessage{my very long message that will be cut on many lines because \LaTeX for some weird reasons like to manually cut the lines instead of letting the terminal do it}

\def\foo{(something in the middle)}

\prefixmessage*[>>>]{my very long message that will be cut on many lines because LaTeX \foo\space for some weird reasons like to manually cut the lines instead of letting the terminal do it}

\stop

Output on the terminal and in the log file

! my very long message that will be cut on many lines because \LaTeX for some
! weird reasons like to manually cut the lines instead of letting the terminal
! do it

>>> my very long message that will be cut on many lines because LaTeX >>> (something in the middle) for some weird reasons like to manually cut the >>> lines instead of letting the terminal do it

egreg
  • 1,121,712
0

Another version with LaTeX3: lines are separated by \robExtPrefixLogMessage (if you use it in your code, choose other names not to collude with my CTAN library), and will prefix it with \robExtPrefixLogMessage.

\def\robExtMessageWithPrefixNumberLines{^^J}%
\cs_new:Nn \robExt__message_with_prefix:n {
  \str_set:Nn \l_tmpa_str {#1}  
  \int_compare:nNnTF {\str_count:N \l_tmpa_str + \str_count:N \robExtPrefixLogMessage} > {78} {
    \typeout{\robExtPrefixLogMessage \str_range:Nnn \l_tmpa_str {1} {78 - \str_count:N \robExtPrefixLogMessage}\robExtMessageWithPrefixNumberLines}
    \robExt__message_with_prefix:x {\str_range:Nnn \l_tmpa_str {78 - \str_count:N \robExtPrefixLogMessage + 1} {-1}}
  }{
    \typeout{\robExtPrefixLogMessage \l_tmpa_str \robExtMessageWithPrefixNumberLines}
  }
}
\cs_generate_variant:Nn \robExt__message_with_prefix:n { x }

tobiasBora
  • 8,684