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}
22
  • 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? May 29, 2020 at 10:14
  • ...or do you mean the end of the paragraph? May 29, 2020 at 10:33
  • I have edited. I hope it is clear now.
    – antshar
    May 29, 2020 at 10:37
  • 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, 2020 at 19:52
  • 2
    @antshar \bfseries certainly does not do what you think. It goes on until the end of the current group.
    – egreg
    May 31, 2020 at 20:32

1 Answer 1

1
+50

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

9
  • 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 :( imgur.com/4vF2dgB
    – antshar
    Jun 8, 2020 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. Jun 8, 2020 at 11:11
  • 1) 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 8, 2020 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. Jun 8, 2020 at 12:07
  • Is it possible to redefine \\ glabaly. Or how can I redefine \\ in them?
    – antshar
    Jun 8, 2020 at 12:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .