GeneralConditional based on E-O-L factor

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
tomasz_zsamot
Posts: 1
Joined: Sun Feb 12, 2012 11:04 pm

Conditional based on E-O-L factor

Post by tomasz_zsamot »

Hello,

I have a problem I'm unable to solve by myself. I need to declare a new command that would do the following:
  1. take two pieces of text (containing also legal LaTeX commands) as arguments
  2. check, whether placing them one after another would lead to crossing the text boundary (the right margin)
  3. if the conditional in point 2. yields "FALSE" as its result, the two pieces of text should be placed one after another with no special action
  4. if, however, the result of the conditional (point 2) is "TRUE", the order of the two pieces of text should be inverted.
  5. Possibility of nesting would be a great advantage.
I need such a command to cope with non-standard typing of bidirectional text in my book. I'll be very grateful indeed if anybody helps me with this problem.


Tomasz

Recommended reading 2024:

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

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

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

Re: Conditional based on E-O-L factor

Post by Stefan Kottwitz »

Hi Tomasz,

welcome to the board!

Unfortunately there's no answer yet. Perhaps provide further information, such as the code which you could produce for that until now, so a reader doesn't have to work it out from scratch.

Stefan
LaTeX.org admin
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Conditional based on E-O-L factor

Post by cgnieder »

One would need to measure the width of both arguments together and compare it to the current \linewidth. This should do it:

Code: Select all

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\newlength{\rvslgth}
\newcommand\reverseif[2]{%
  \settowidth{\rvslgth}{#1 #2}%
  \ifdim\rvslgth>\linewidth
    #2 #1%
  \else
    #1 #2%
  \fi}

\begin{document}
% nothing:
\reverseif{First}{Second}

% reversed:
\reverseif{A longer example containing a macro  (\LaTeX) and other stuff.}{This will make sure both arguments together are longer than one line.}

% nested:
\reverseif{First}{\reverseif{A longer example containing a macro  (\LaTeX) and other stuff.}{This will make sure both arguments together are longer than one line.}}

\end{document}
Regards
site moderator & package author
Post Reply