1

I would like to create a macro command that would put everything inside brackets for example or any other character, till the end of a line or till the end of a group {\inchar ...}. Something like a custom \bfseries command. Using default commands or xparse. It is interesting to see several implementation if possible.

That is, such an inputs should produce corresponding outputs


text text \inchar() text text text text

some other text on a new line

                      

text text (text text text text)
some other text on a new line

text text \inchar|] text text text text\\
some other text on a new line

                      

text text |text text text text]
some other text on a new line

text text \inchar\{\} text text text text\par
some other text on a new line

                      

text text {text text text text}
some other text on a new line

Also it should work in amsmath environments

\begin{multline*}
abc\\
\inchar() def\\
ghi \inchar\{\} jkl + mno
\end{multline*}

                      

abc
                                          (def)
                                                                           ghi {jkl + mno}
antshar
  • 4,238
  • 9
  • 30
  • One point is not wholly clear. By "end of the line", do you mean the end of the line in the input file, or do you mean the end of the line in the typeset output? – Steven B. Segletes May 29 '20 at 10:14
  • ...or do you mean the end of the paragraph? – Steven B. Segletes May 29 '20 at 10:33
  • I have edited. I hope it is clear now. – antshar May 29 '20 at 10:37
  • It is not clear. End of input line or end of output line? – wipet May 29 '20 at 13:19
  • @wipet of couse the .pdf is meant. – antshar May 29 '20 at 13:29
  • When macro is expanded then TeX cannot know where the line in the PDF output will be end. The paragraph breaking algorithm will be started later. – wipet May 29 '20 at 13:51
  • Sorry, but I cannot make a sense out of the request. – egreg May 31 '20 at 19:41
  • @egreg, I just don't want to make it complicated so the post contain a simple version of what I actually need, but with the solution I will be able to modify that for myself. – antshar May 31 '20 at 19:45
  • 1
    @antshar You've been asked to explain what you mean by “end of the line”, but you ignored what's really the main point towards a possible solution. Anyway, I see no motivation for the problem. If you want to fence some text, do it explicitly. – egreg May 31 '20 at 19:52
  • @egreg, by "end of line" I meant an end of a line after \inchar. A great example would be \bfseries it makes everything bold till the end of a line in your tex editor(line wrapping is not counting). I can be more speciefic and tell the actual porpuse of this question. I am looking for the solution to this problem: https://tex.stackexchange.com/q/546196 even with a bounty I have no luck getting an answer. I kinda thought up with a workaround that requires something that I am asking in this post. – antshar May 31 '20 at 20:11
  • 2
    @antshar \bfseries certainly does not do what you think. It goes on until the end of the current group. – egreg May 31 '20 at 20:32
  • @egreg yeah, I know. But if you don't put backets around \bfseries and some text it will affect everything till the end of a line. – antshar May 31 '20 at 20:34
  • @egreg, seems like I almost found the solution: https://tex.stackexchange.com/q/10426 The only problem is that it doesn't work if the line ends with \\ or \par. Could you help me in tweaking that? – antshar Jun 03 '20 at 16:36
  • @antshar What is the expected precedence of \\, \par and newline character? For example, in text text<newline> text \\ text and text \\ text<newline>, which newline-like input is taken as the marker? – muzimuzhi Z Jun 05 '20 at 18:55
  • @muzimuzhiZ the closest one. Either new line made by double pressing of Enter, or \\, or\par. – antshar Jun 06 '20 at 10:47
  • @antshar From your examples in question, I understood that you want to use single pressing of Enter as the marker of new line. Single Enter means <line 1><Enter><line 2>, while double Enter means <line 1><Enter><empty line 2><Enter><line 3>. Which one do you want? – muzimuzhi Z Jun 06 '20 at 12:03
  • @muzimuzhiZ single pressing of Enter in your LaTeX editor won't make a new line in the output file. So I meant a double pressing of the Enter which results an appearing of a new line. – antshar Jun 06 '20 at 13:33
  • @antshar 1) So you are talking about the new line or end of paragraph in output, inserted either automatically by TeX or manually by user. 2) But the linked question (https://tex.stackexchange.com/q/10426) actually only deals with the new line character in the input source. That misleads me a lot. – muzimuzhi Z Jun 06 '20 at 14:08
  • @antshar 3) At last, in your previous comment, you said "But if you don't put brackets around \bfseries and some text it will affect everything till the end of a line." That is not true. \bfseries will affect everything till the end of the current group, for example the end of document. Try text \bfseries text <newline><newline> text text. – muzimuzhi Z Jun 06 '20 at 14:08
  • @muzimuzhiZ oh... sorry, my bad about \bfseries distribution to next lines. I don't know how to make it more clear, but I think you got me, "1)" says exactly what I would like to achieve. – antshar Jun 06 '20 at 14:22
  • @antshar Then, your needs cannot be fulfilled by normal TeX utilities (and techniques). I am not familiar with LuaTeX, maybe it is doable with Lua callbacks. Maybe you can post a new question, asking if LuaTeX can do that and how. – muzimuzhi Z Jun 06 '20 at 15:29
  • @muzimuzhiZ is it possible, though, to make take it work at least with \\ \par? – antshar Jun 07 '20 at 02:12

1 Answers1

1

A first try: \autoScoped{<arg>} opens a scope which can be auto ended by \\ or \par, and then uses <arg>. Note this is not the usual way that LaTeX should be used and this first try is far from complete and might break many things.

Support for lists (including trivlist) is added. After trying, I find I am not capable of patching amsmath package yet. Sorry.

\documentclass{article}
\usepackage{multicol}
\usepackage{xpatch}

\makeatletter
\newif\if@inscope

% start a local scope, nesting is not supported
\def\autoScoped{\@inscopetrue\ifmmode[\else\bfseries\fi}
% end a local scope
\def\end@scope@if@in{%
  \if@inscope
    \ifmmode]\else\mdseries\fi
    \@inscopefalse
  \fi}

% patch variants of \\
\@tfor\@tempa:=%
  \@normalcr % normal \\
  \@centercr % inside trivlist
\do{%
  \expandafter\xpretocmd\@tempa
    {\end@scope@if@in}
    {}{\fail}%
}
\let\\\@normalcr

% patch \par
\def\par@delimited{\end@scope@if@in\@@par}
\let\par\par@delimited
\def\@par{\let\par\par@delimited\par}
\def\@setpar#1{%
  \def\par{\end@scope@if@in#1}\def\@par{\end@scope@if@in#1}}
\makeatother


\begin{document}

\newcommand\test[1][]{
  % case 1: delimited by \\
  #1\normalfont abc def \autoScoped ghi \\ jkl \par
  % case 2: delimited by explicit \par
  #1\normalfont abc def \autoScoped ghi \par jkl \par
  % case 3: delimited by implicit \par (consecutive newlines)
  #1\normalfont abc def \autoScoped ghi

  jkl \par
}

\setlength{\columnseprule}{.4pt}
\begin{multicols}{2}  
  \subsection*{Normal case}
  \test

  \subsection*{Inside trivlist and list}
  \begin{enumerate}
    \test[\item]
  \end{enumerate}

  \begin{center}
    \test
  \end{center}

  {\raggedleft \test \par}
\end{multicols}

\end{document}

enter image description here

muzimuzhi Z
  • 26,474
  • I think the third example should be with a double Enter new line. Wow, it works great. However, if I replace \bfseries and \mdseries with [ and ] respectively a space before ] appers. Also it doesn't work in amsmath environments, unfortunately :( https://imgur.com/4vF2dgB – antshar Jun 08 '20 at 10:47
  • @antshar 1) No, my answer cannot handle double Enter new line. 2) Generally, \autoScoped and \end@scope@if@in can only contain declarative commands, which means it should not produce any output. 3) Yes, and there are many many more cases (in center env, in tabular env, ...) it doesn't work. As I pointed in my answer, you almost should not use latex in this way. – muzimuzhi Z Jun 08 '20 at 11:11
  • What do you mean? It works great for me with double Enter new line. 3) Why doesn't it work in those environments?
  • – antshar Jun 08 '20 at 11:59
  • @antshar 1) TeX automatically converts consecutive new line characters to \par, so the patch to \par works. 2) Because in those environments, the definition of \\ or \par (or both of them) is changed. – muzimuzhi Z Jun 08 '20 at 12:07
  • Is it possible to redefine \\ glabaly. Or how can I redefine \\ in them? – antshar Jun 08 '20 at 12:17
  • @antshar Support for list and amsmath envs is added. – muzimuzhi Z Jun 08 '20 at 15:10
  • Great! Almost perfect. Just to note, the original question is about adding characters from both sides. \bfseries was just for an example. But that is not the problem, because I can easily replace the condition with desired character. The problem that currently is – if you try to add this on the last line of the multline environment, bracket appears at the different spot: https://imgur.com/VV4dY3B         And also if I try to use several of commands on one line a b \autoScoped c d \autoScoped e f one bracket is missing: https://imgur.com/whiFagV – antshar Jun 08 '20 at 16:00
  • Despite having no the perfect solution. You were the only who left an answer, so I will give you the points. – antshar Jun 12 '20 at 08:55