1

Goal: create a command that works within a group (a scope)

I am using polyglossia package to write bilingual texts. I want to create \eng declaration or switch to behave just like \textenglish:

{\eng this is an english text} this is a foreign text

This should be equivalent to:

\textenglish{this is an english text} this is a foreign text
AvidSeeker
  • 37
  • 6
  • 3
    All macros which do not perform global changes act only within their group. What is \mycmd supposed to do? – campa Mar 02 '21 at 12:35
  • \mycmd is just any command that effect what is inside the group. – AvidSeeker Mar 02 '21 at 12:43
  • 1
    Recall, \color, \bfseries, or tiny don't take arguments (they are declarations or "switches", if you will). If your \mycmd also doesn't take an argument, they you are good to go without anything special. If \mycmd takes an argument, then it is not "just like" those other macros. – Steven B. Segletes Mar 02 '21 at 12:43
  • Basically, the answer depends on what \mycmd wants to do. If you give an example... – Rmano Mar 02 '21 at 12:52
  • 1
    obeying grouping is the default behaviour so your question as asked doesn't really have any answer. Note that \color, \bfseries etc have no access to the text that is in the current group, they just change the state and that state ends when the group ends. – David Carlisle Mar 02 '21 at 12:53
  • 2
  • \newcommand\mycmd{\bfseries\itshape\color{red}} and then do {\mycmd abc is some text}, does what you want. If whatever you want to achieve is achievable just by changing some states (like the colour stack, the font in use, some sizes, etc.) you can do this just by changing the definition. If your macro needs access to the contents of the group, then just as others have already said, that's impossible. – Skillmon Mar 02 '21 at 13:33
  • Hello everyone. Thanks for your help. I have updated my question to give a concrete example of what exactly I want. – AvidSeeker Mar 02 '21 at 13:47

1 Answers1

3

polyglossia provides the macro \text⟨language name⟩{⟨text⟩} which takes the text as argument, and the switch \selectlanguage{⟨language name⟩}. That's the one you want to use.

\documentclass{article}

\usepackage{polyglossia} \setmainlanguage{german} \setotherlanguage{english}

\newcommand*{\eng}{\selectlanguage{english}}

\begin{document}

\noindent Das ist auf Deutsch: \figurename. {\eng This is english: \figurename.} Das ist wieder auf Deutsch: \figurename.

\end{document}

enter image description here

campa
  • 31,130
  • Thank you. This solves my problem. But I have posted my question initially as a general question about creating a switch from a command. I mean, how can I do this for other commands from different packages? Is there a recipe for it? – AvidSeeker Mar 02 '21 at 14:10
  • 1
    @AvidSeeker We have tried to explain to you in comments that there is no general answer to this. It depends on how the macro you are interested in has been defined to begin with. As long as you have only switches, they will always act locally. But does the macro take argument(s)? Well, then it depends on how they are processed. – campa Mar 02 '21 at 14:12