LyXhow to switch the language between text and math?

Information and discussion about LyX, a WYSIWYM editor, available for Linux, Windows and Mac OS X systems.
Post Reply
ciradu
Posts: 6
Joined: Thu Dec 06, 2012 6:44 pm

how to switch the language between text and math?

Post by ciradu »

I need to know if there is a posibility for automaticaly change the language between text and math mode. I use romanian in text, but I need english when I insert math formula because of the keyboard layout diferences.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

how to switch the language between text and math?

Post by Stefan Kottwitz »

There are two hooks which can be used:
  • \everymath is called every time when it's switched to inline math,
  • \everydisplay is called every time when it's switched to displayed math, for example in equation environments.
Here's a complete demonstration example, where English is used for normal text, and German is used for math (and text in math):

Code: Select all

\documentclass{article}
\usepackage[ngerman,english]{babel}% loading two languages
\everymath{\selectlanguage{ngerman}}
\everydisplay{\selectlanguage{ngerman}}
\usepackage{amsmath}
\usepackage{parskip}
\begin{document}
tes"-ting english% ngerman shortcut "- doesn't work, but is visible instead
                 % i.e. ngerman is not active, but english

$\text{tes"-ting ngerman}$% here, "- works and is hidden, ngerman is active

tes"-ting english% again, ngerman shortcut "- doesn't work, english is active
\[ \text{tes"-ting ngerman} \]% also here, "- works and is hidden, ngerman is active
tes"-ting english% ngerman shortcut "- doesn't work any more, english is active again
\end{document}
language-switching.png
language-switching.png (6.66 KiB) Viewed 6172 times
Stefan
LaTeX.org admin
ciradu
Posts: 6
Joined: Thu Dec 06, 2012 6:44 pm

Re: how to switch the language between text and math?

Post by ciradu »

Thank you for your effort, but I don't know how to implement your solution to make it work in my documents.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

how to switch the language between text and math?

Post by cgnieder »

You need to add

Code: Select all

\usepackage[ngerman,english]{babel}% last language in list will be document language
\everymath{\selectlanguage{ngerman}}
\everydisplay{\selectlanguage{ngerman}}
(or which language you want to have inside math) to the document preamble under Document>Settings>LaTeX Preamble.

Make sure you also choose language=none in the language settings.

Regards
site moderator & package author
ciradu
Posts: 6
Joined: Thu Dec 06, 2012 6:44 pm

Re: how to switch the language between text and math?

Post by ciradu »

I did this, but I can't see any difference. It doesn't automaticaly change my keyboard layout.
Thanks.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Re: how to switch the language between text and math?

Post by Stefan Kottwitz »

The code above changes the language support, such as hyphenation. However, the keyboard layout depends on your operating system and this cannot be changed by LaTeX, except making external calls to a software which changes the keyboard layout. You would need such a software first.

Stefan
LaTeX.org admin
ciradu
Posts: 6
Joined: Thu Dec 06, 2012 6:44 pm

Re: how to switch the language between text and math?

Post by ciradu »

Now I understand. Thanks for your help.
Post Reply