For "academical" purpose, this is a macro that automatically braces the next Unicode character.
It does requires patching \big
however. Unavoidably.
How TeX input stream works is a bit complex, you still need TeX knowledge to use this macro i.e. it's not 100% automatic.
It only works in pdflatex -- nevertheless if you're using some Unicode engine you would not need this code at all.
Side note--if you use this in a package/extend the code etc., fix the naming convention of \__stored_content
etc. yourself, see expl3 manual interface3.pdf.
(use \tl_analysis_map_inline:nn
just to check if it's an active character instead of something like e.g. \str_count:n
, to handle
some unlikely case that there's a length-1 control sequence and \escapechar=-1
...)
%! TEX program = pdflatex
\documentclass{article}
\usepackage{newunicodechar}
\errorcontextlines=100
\newunicodechar{‖}{\ensuremath{\|}}
\begin{document}
\ExplSyntaxOn
% command docs:
% if you execute `\__brace_next_unicode:nw {blob blob} ■` where ■ is any
% multi-byte UTF8 character, after some execution steps `blob blob {■}`
% will be executed. (spaces are only for demonstration.)
\cs_new_protected:Npn \__brace_next_unicode:nw #1 {
\peek_N_type:TF {
\tl_set:Nn \__stored_content {#1} % actually this part can be done expandably
% as well... but \peek_N_type:TF is already unexpandable
\__brace_next_unicode_get_one_byte:N
}
{
% do nothing, just put `blob blob` out
#1
}
}
\cs_new_protected:Npn \__brace_next_unicode_get_one_byte:N #1 {
\tl_analysis_map_inline:nn {#1} {
\bool_set:Nn \__is_active_character { \token_if_eq_charcode_p:NN ##3 D }
}
\bool_if:nTF \__is_active_character {
\int_compare:nNnTF {`#1} < {"80} {
% not a part of multibyte UTF8 character, put it back.
\__stored_content #1
} {
% part of multibyte UTF8 character.
\int_compare:nNnTF {`#1} < {"E0} {
% 2 bytes
\__brace_next_unicode_handle_two:nn #1
} {
\int_compare:nNnTF {`#1} < {"F0} {
% 3 bytes
\__brace_next_unicode_handle_three:nnn #1
} {
% 4 bytes
\__brace_next_unicode_handle_four:nnnn #1
}
}
}
}
{
% else, it could be a control sequence or similar. Do nothing with it.
\__stored_content #1
}
}
\cs_new_protected:Npn \__brace_next_unicode_handle_two:nn #1 #2 {
\__stored_content {#1 #2}
}
\cs_new_protected:Npn \__brace_next_unicode_handle_three:nnn #1 #2 #3 {
\__stored_content {#1 #2 #3}
}
\cs_new_protected:Npn \__brace_next_unicode_handle_four:nnnn #1 #2 #3 #4 {
\__stored_content {#1 #2 #3 #4}
}
%\__brace_next_unicode:nw {\pretty:nn {123}} ■
\NewCommandCopy \oldbig \big
\def \big {\__brace_next_unicode:nw {\oldbig}}
\ExplSyntaxOff
\[ ‖x‖ \]
\[ \big‖x\big‖ \]
% check that it still works in normal cases
\[ \big|x\big| \]
\[ \big|x\big| \]
\[ \big{|}x\big{|} \]
\[ \big{|}x\big{|} \]
\[ \big\lbrace x\big\rbrace \]
\end{document}
‖
character automatically determine whether the spacing should be\mathopen
,\mathclose
, as a middle bar or a relational operator. However, you might use the paired-math-delimiter commands inmathtools
for a good shortcut.