3

I was writing part of a slightly mathematical paper in LaTeX and I came across the following problem. I worked out an alternative (and good) solution to my problem, but I'm very curious if anyone knows how to do this (MWE at the end of the post).

I want to be able to add superscript and subscript to a custom command that I made for the paper. Consider the following command:

\NewDocumentCommand \conceptsymbol {} {L}
\NewDocumentCommand \conceptvalue { m O{\conceptsymbol} } { #2(#1) }

I made this because the paper is still in early stages and we might change the symbols. Consider now the following four possibilities ($ omitted for brevity's sake)

\conceptsymbol, \conceptsymbol', \conceptsymbol_1 and \conceptsymbol_1'.

These would be rendered as L, L', L_1 and L_1'. I have no problems with this, however, I would like to refer to some property of each concept using the conceptvalue. I can easily change the symbol used in the conceptvalue as optional parameter. I can use the default parameter

\conceptvalue{i}  % L(i)

or I can set a different value

\conceptvalue{i}[\conceptsymbol']  % L'(i)
\conceptvalue{i}[\conceptsymbol_1]  % L_1(i)
\conceptvalue{i}[\conceptsymbol_1']  % L_1'(i)

I would like to do the same but in a shorter way. For example, I could write

\conceptvalue{i}'  % L(i)'
\conceptvalue{i}_1  % L(i)_1
\conceptvalue{i}_1'  % L(i)_1'

but this does not produce the correct render of the symbols. Does somebody know how to shorten this? Thanks to you all!

Full MWE:

\documentclass[12pt]{article}
\pdfoutput=1

\usepackage{amsmath} \usepackage{amssymb} \usepackage{xparse} \usepackage{booktabs} \usepackage{multicol}

\NewDocumentCommand \conceptsymbol {} {L} \NewDocumentCommand \conceptvalue { m O{\conceptsymbol} } { #2(#1) }

\begin{document} \allowdisplaybreaks

\title{The hardest part of any paper is dealing with \LaTeX} \author{Me, myself, and some nice {\tt tex.stackexchange} users}

\maketitle

\section{Please, help}

My commands are: \begin{verbatim} \NewDocumentCommand \conceptsymbol {} {L} \NewDocumentCommand \conceptvalue { m O{\conceptsymbol} } { #2(#1) } \end{verbatim} Assume that $\conceptsymbol$ refers to a sequence of $n$ elements and that $\conceptvalue{i}$ denotes the $i$th element in the sequence. Suppose now that I have several sequences $\conceptsymbol$, $\conceptsymbol'$, $\conceptsymbol_1$ and $\conceptsymbol_1'$.

{\small \begin{table}[h] \centering \begin{tabular}{cl}

    \toprule
    \multicolumn{2}{c}{What I have} \\
    \midrule

    $\conceptvalue{i}$ & \verb|\conceptvalue{i}| \\
    $\conceptvalue{i}'$ & \verb|\conceptvalue{i}'| \\
    $\conceptvalue{i}_1$ & \verb|\conceptvalue{i}_1| \\
    $\conceptvalue{i}_1'$ & \verb|\conceptvalue{i}_1'| \\

    \midrule

    $\conceptvalue{i}$ & \verb|\conceptvalue{i}| \\
    $\conceptvalue{i}[\conceptsymbol']$ & \verb|\conceptvalue{i}[\conceptsymbol']| \\
    $\conceptvalue{i}[\conceptsymbol_1]$ & \verb|\conceptvalue{i}[\conceptsymbol_1]| \\
    $\conceptvalue{i}[\conceptsymbol_1']$ & \verb|\conceptvalue{i}[\conceptsymbol_1']| \\

    \bottomrule
\end{tabular}
\quad
\begin{tabular}{cl}
    \toprule
    \multicolumn{2}{c}{Shorter way?} \\
    \midrule

    $\conceptsymbol(i)$ & ? \\
    $\conceptsymbol'(i)$ & ? \\
    $\conceptsymbol_1(i)$ & ? \\
    $\conceptsymbol_1'(i)$ & ? \\

    \bottomrule
\end{tabular}

\end{table} }

\end{document}

enter image description here

  • Okay it's "relatively" simple but... do you want to learn to program in TeX, program in Lua, or just want some copyable code? – user202729 Mar 27 '22 at 17:01
  • Although why not just type \conceptsymbol'(i) directly? What's the point of the conceptvalue macro? (could the type of bracket change as well?) – user202729 Mar 27 '22 at 17:05
  • Well, this way I can reuse it in other parts of the document. I wish to learn more TeX so I don't have to ask again and bother people with things that I could solve if I knew more of it. Also, it will give me more freedom to apply it to other macros in the future for which I can't just copy and modify the code people may give here. – Lluís Alemany-Puig Mar 27 '22 at 18:42
  • The person that wrote an answer to this question (very useful!) has decided to remove it. Could that person, please, add it again? I really think I can learn from that. – Lluís Alemany-Puig Mar 28 '22 at 07:33
  • Maybe just wait a bit, if they fix they can undelete (I left a comment there point out a possible issue) – user202729 Mar 28 '22 at 08:17
  • Ok, thank you for your contributions! :) – Lluís Alemany-Puig Mar 28 '22 at 12:44
  • By the way if you want to learn TeX programming (I recommend people to just learn Lua/Python etc. and use it inside TeX but well...) I'd recommend you to read the book and learn properly anyway. TeX is full of unexpected behaviors, and not knowing some will be highly problematic. – user202729 Mar 28 '22 at 12:46
  • I'm not looking to become a TeX programmer, so if I prefer learning the bits that I need just for this setback and perhaps a bit more with complementary reading, rather than reading a whole book. The post asked, "how to do ...?" rather than "Where do I start reading to become an expert in TeX programming?". – Lluís Alemany-Puig Mar 29 '22 at 15:31
  • I think TeX is such a weird, unintuitive, different-from-normal language that you probably will spend a lot of time frustratingly debug your macro (like how I did) // by the way the previous answer uses \def and "brace delimiting" technique (see e.g. TeX by topic 11.5.6 "Brace delimiting") to allow the syntax e.g. \conceptvalue'{i} (the ' goes into a parameter), then I point out that it cannot do \conceptvalue_{12}{i} because it captures up to the first brace → only capture the _ – user202729 Mar 30 '22 at 06:06

0 Answers0