0

I want to assign a variable dynamically, based on the maximum of two values. I have seen a max function used here: https://tex.stackexchange.com/a/505352/194783. I cannot see how it is used, though.

Also, I want to know about other options for this and what is the best one to use.

Related questions:

clel
  • 325
  • Is ipavlic's answer in the thread you linked to not applicable or unclear? If so, explain why not, and explain a bit more about your use case. Are these "variables" lengths? Or counters? Or something else? – frabjous Jun 06 '22 at 18:05
  • I want to know the best way. But yes, ideally, that answer would work. Apparently, for my use case, it doesn't (see https://tex.stackexchange.com/questions/430402/tikz-pgf-and-maxof-broken-since-3-0-1a).

    Also, I didn't really find a good result on Google for this general question that I had. So that is why I created this question.

    – clel Jun 06 '22 at 18:08

2 Answers2

1

You can create the macro \maxvalue for example:

\def\maxvalue(#1,#2){\ifnum #1>#2 #1\else #2\fi}

\newcount\num \num=\maxvalue(10,11)

\the\num % prints 11

\bye

wipet
  • 74,238
  • When I tried it, it didn't work for me (maybe only for my use case, not sure). A different version of this worked, though. – clel Jun 06 '22 at 20:19
  • @clel Please, be more specific. I.e. show an example where it didn't work. – wipet Jun 07 '22 at 03:55
  • I wanted to use this function to assign a style value for a tikzpicture. I just created a MWE, though, to test the general functionality of your approach. In the MWE, it works, so I guess this is related to the usage in the scope of TikZ. – clel Jun 07 '22 at 07:42
  • BTW: Is \bye a command that you use, or did you include it just for fun? – clel Jun 07 '22 at 07:43
  • @clel \bye is a Plain TeX macro which ends the document. This example is plain TeX but you can include it to a LaTeX document too (without the \bye). – wipet Jun 07 '22 at 13:07
  • Ah, alright. I am using LaTeX, thus I wasn't aware of that. – clel Jun 07 '22 at 13:08
  • You was asking for "the best one to use". I show the example independent of the used format (i.e. independent of LaTeX macros). This is IMHO "best one to use" because it works in all formats (plain TeX, LaTeX, ConTeXt, OpTeX, ...) because it is based only on TeX primitives. – wipet Jun 07 '22 at 13:12
0

You can exploit the fact that lengths in TeX are always normalized in pt and that l3-fp converts lengths to their value in points as well.

\documentclass{article}

\ExplSyntaxOn

\NewExpandableDocumentCommand{\maxlength}{m} { \fp_eval:n { max(#1) } pt }

\ExplSyntaxOff

\newlength{\testlen}

\begin{document}

\setlength{\testlen}{\maxlength{1pt,2pt,3pt}}

\the\testlen

\maxlength{45pt,0.5\textwidth,20cm}---\the\dimexpr20cm\relax

\maxlength{45pt,3\textwidth,20cm}---\the\dimexpr3\textwidth\relax

\edef\test{\maxlength{600pt,\textwidth,6cm}}

\texttt{\meaning\test}

\end{document}

enter image description here

As usual, for older versions of LaTeX you might need \usepackage{xparse}.

egreg
  • 1,121,712