1

Consider the following MWE:

\documentclass{article}
\usepackage{xparse,l3draw,xcolor}

\ExplSyntaxOn
%-----------------------------------------------------------------------------
% Error handling function warning message

\msg_new:nnn { mathicon } { invalid-size }
 { Size~must~be~positive.~Using~default~of~11pt.}

\msg_new:nnn { mathicon } { invalid-shape }
 { The~shape~"#1"~is~not~recognized.~Using~a~square~by~default. }
 

%----------------------------------------------------------------------------- 
% Error handling function for size
\cs_new_protected:Nn \__mi_geo_check_size:n
 {
  \dim_compare:nNnTF {#1} > { 0pt }
   { \dim_set:Nn \l__mi_geo_size_dim {#1} }
   {
    \msg_warning:nn { mathicon } { invalid-size }
    \dim_set:Nn \l__mi_geo_size_dim { 11pt }
   }
 }
%-----------------------------------------------------------------------------
% Command to draw the shape with optional size, color, and line thickness
\NewDocumentCommand{\migeoshape}{O{ 11pt } O{ magenta } O{ 0.025 } m}
 {
  \mi_geo_draw_shape:nnnn { #4 } { #1 } { #2 } { #3 }
 }
%----------------------------------------------------------------------------- 
% Internal variables
\dim_new:N \l__mi_geo_size_dim

% Function to draw square
\cs_new_protected:Nn \mi_geo_draw_square:
 {
  \draw_path_rectangle:nn { 0.0\l__mi_geo_size_dim , 0.0\l__mi_geo_size_dim } { 1.0\l__mi_geo_size_dim, 1\l__mi_geo_size_dim }
  \draw_path_use_clear:n { stroke }
 }

%-----------------------------------------------------------------------------
% Main function to draw the shape with size, color, and line thickness
\cs_new_protected:Nn \mi_geo_draw_shape:nnnn
 {
  \__mi_geo_check_size:n { #2 }
  \draw_begin:
  \color_select:n { #3 }
  \draw_linewidth:n { #4 \l__mi_geo_size_dim }
  \str_case:nnF { #1 }
   {
    { square } { \mi_geo_draw_square: } %Use for cases. This is just for testing.
   }
   {
    \msg_warning:nnn { mathicon } { invalid-shape } { #1 }
    \mi_geo_draw_square:
   }
  \draw_end:
 }
\ExplSyntaxOff

\begin{document}
\migeoshape[-11pt][red][0.036]{square} Sample
\migeoshape[11pt][black][0.075]{hexagon} % This will default to square with a warning
\end{document}

The first warning works well, but for the second something weird happens. In the image below, (mathicon) appears in the message.

img1

Also, if I find the message in Texmaker, the part (mathicon) square by default. goes to the next line and is not highlighted blue.

img2

Is there a fix to this? Also I would like to add the line number to the warming message. I have not managed to succesfully use \msg_line_context:

Also, if you have any suggestions for my code improvement, let me know.

3
  • 2
    latex is adding line breaks and prefixing the new lines with a package name (it does something similar with standard latex2e warnings, without using expl3). It appears that your editor isn't correctly handling the newlines, so you would need to fix that in the editor not in tex. Commented Aug 24, 2024 at 20:21
  • Thanks for the prompt feedback @DavidCarlisle . Any idea why in my second warning message `(mathicon) appears? Commented Aug 24, 2024 at 20:27
  • 3
    look in the log and as you show in the second form, latex linebreaks long messages but puts the (packagename) prefix at the start of every line, but in the first screenshot the editor has messed up the message removing the linebreaks but leaving the line prefix and in the second screenshot it has messed up the formatting, in both cases the log file as written by latex has the intended format it's just editor bugs in making the log summary Commented Aug 24, 2024 at 20:49

1 Answer 1

1

David already answered the first part of your question, so this answer addresses only the second concerning the addition of line numbers.

The following works for me:

\msg_new:nnn { mathicon } { invalid-shape }
{ The~shape~"#1"~is~not~recognized~on~input~line~  \msg_line_number:.~Using~a~square~by~default. }

Output on terminal:

Package mathicon Warning: The shape "hexagon" is not recognized on input line
(mathicon)                65. Using a square by default.

Alternatively,

\msg_new:nnn { mathicon } { invalid-shape }
{ The~shape~"#1"~is~not~recognized~  \msg_line_context:.~Using~a~square~by~default. }

produces

Package mathicon Warning: The shape "hexagon" is not recognized on line 65.
(mathicon)                Using a square by default.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.