# Optidef constraint name not aligned properly

I have tried to follow optidef.tex to make the constraint names aligned. Here is the code:

\begin{mini*}|s|
{\substack{\mathbf{x},\mathbf{u}}}{J = \phi\left(\mathbf{x}\left(t_f\right),t_f\right) + \int_{t_0}^{t_f}\mathcal{L}\left(\mathbf{x},\mathbf{u},t\right)dt}{}{}
\end{mini*}


And here is the corresponding output that is circled:

Can some explain what is going on? I am writing this out in TeXpad.

• You just need to move the position of the = sign in \addConstraint{\dot{\mathbf{x}} =}{\mathbf{f}\left(\mathbf{x},\mathbf{u},t\right)} to become \addConstraint{\dot{\mathbf{x}}}{ =\mathbf{f}\left(\mathbf{x},\mathbf{u},t\right)}. – Schrödinger's cat Feb 11 at 6:57
• I tried, but it didn’t work – Superman Feb 11 at 7:39
• Do the provided answers solve your problem? If yes, then consider upvoting and accepting one of the answers. – Dr. Manuel Kuehner Mar 8 at 8:39

I'll be happy to remove this post. According to what I find, you just need to move the position of the = sign in

\addConstraint{\dot{\mathbf{x}} =}{\mathbf{f}\left(\mathbf{x},\mathbf{u},t\right)}


to become

\addConstraint{\dot{\mathbf{x}}}{ =\mathbf{f}\left(\mathbf{x},\mathbf{u},t\right)}


Full example:

\documentclass{article}
\usepackage{optidef}
\begin{document}
\begin{mini*}|s|
{\substack{\mathbf{x},\mathbf{u}}}{J = \phi\left(\mathbf{x}\left(t_f\right),t_f\right) + \int_{t_0}^{t_f}\mathcal{L}\left(\mathbf{x},\mathbf{u},t\right)dt}{}{}
\end{mini*}
\end{document}


There are many things one could improve like the \substack or the differential d.

• +1. Another way in which the code could be improved is by omitting all \left and \right auto-sizing instructions, the more so as these instructions don't do anything except insert undesirable horizontal whitespace. – Mico Feb 11 at 9:20
• I have a strong feeling that Texpad is being very glitchy. What software did you use, Schrödinger’s cat? – Superman Feb 11 at 16:55
• @Superman pdflatex. – Schrödinger's cat Feb 11 at 16:56
• @Mico Sure. I was just writing this because I was told my suggestion would not work. (If I was you I would make the differential d's upright in your answer. This seems to be some physics problem. Even though there are some claims that some mathematicians would not use upright upright d's (I know and interact with many, and anyone consciously typing uses an upright d), in physics it is unambiguous. – Schrödinger's cat Feb 11 at 16:59
• @Mico I had upvoted it already... – Schrödinger's cat Feb 11 at 21:46

Just for reference, here's a solution which does not employ the optidef package. Instead, it employs 1 align* environment (for the 4 "outer" blocks) and 1 aligned environment (for the group of 3 "inner" blocks which, together, make up the final "outer" block). align* and aligned are both provided by the amsmath package.

\documentclass{article}
\usepackage{amsmath,amssymb,bm}
\newcommand{\diff}{\mathop{}\!\mathrm{d}} % see https://tex.stackexchange.com/a/385862/5001
\newcommand\tf{t\mkern-1.5mu_f} % snug up the subscript-f to the t

\begin{document}
\begin{align*}
&\text{PM: } \textstyle
J=\phi(\mathbf{x}(\tf),\tf)
+\int_{t_0}^{\tf}\!\mathcal{L}(\mathbf{x},\mathbf{u},t)\diff t \\
&\text{General Problem: } \textstyle
\min J=\phi(\mathbf{x}(\tf))
+\int_{t_0}^{\tf}\! \mathcal{L}(\mathbf{x},\mathbf{u},t)\diff t\\
J=\phi(\mathbf{x}(\tf),\tf)
+\int_{t_0}^{\tf} \!\! \mathcal{L}(\mathbf{x}, \mathbf{u},t)\diff t \\
\begin{aligned}[t]
\dot{\mathbf{x}} &= \mathbf{f}(\mathbf{x},\mathbf{u},t) \\
\end{aligned}
\end{align*}

\end{document}


I cannot reproduce the output you get with your code.

Besides moving the = to the second argument to \AddConstraint, as suggested in another answer, I propose a different setting, with constraints left aligned, which seems better to me.

Also I removed all \left/\right pairs, which do nothing besides adding unwanted space. A \, before dt is necessary, instead. Whether you want an upright “d” or not is your choice: conform to what is the usage in your field (or to your personal preference).

\documentclass{article}
\usepackage{amsmath,optidef}

\begin{document}

\subsection*{Original}

\begin{mini*}|s|
{\substack{\mathbf{x},\mathbf{u}}}{J = \phi\left(\mathbf{x}\left(t_f\right),t_f\right) + \int_{t_0}^{t_f}\mathcal{L}\left(\mathbf{x},\mathbf{u},t\right)dt}{}{}
\end{mini*}

\subsection*{Left alignment}

\begin{mini*}|s|
{\scriptstyle\mathbf{x},\mathbf{u}}
{J = \phi(\mathbf{x}(t_f),t_f) + \int_{t_0}^{t_f}\mathcal{L}(\mathbf{x},\mathbf{u},t)\,dt}{}{}
\end{mini*}

\subsection*{Alignment at equals signs}

\begin{mini*}|s|
{\scriptstyle\mathbf{x},\mathbf{u}}
{J = \phi(\mathbf{x}(t_f),t_f) + \int_{t_0}^{t_f}\mathcal{L}(\mathbf{x},\mathbf{u},t)\,dt}{}{}
\end{mini*}

\end{document}


In my opinion, optidef should have an option to make the subscript variables in script size instead of normal size. I opted for a simpler \scriptstyle declaration in place of the more complicated \substack.

As a further simplification for inputting your formulas, I suggest to add something like

\newcommand{\vectorvariable}[1]{\mathbf{#1}}
\newcommand{\vf}{\vectorvariable{f}}
\newcommand{\vu}{\vectorvariable{u}}
\newcommand{\vx}{\vectorvariable{x}}


so, for instance, the first constraint would become

\addConstraint{}{\dot{\vx}}=\vf(\vx,\vu,t)}{}


In case a reviewer tells you that vector variables should be in bold italic, you'll just have to add

\usepackage{bm}


and change a single line in your code, namely

\newcommand{\vectorvariable}[1]{\bm{#1}}


After this, running LaTeX on your document would yield

(or the other layout, according to the one you prefer, of course). Similarly, adding

\newcommand{\diff}{\mathop{}\!d}


and typing \diff t when you want the differential, will allow you to cope with fussy reviewers or supervisors requiring an upright “d” by just changing a single line of code. Note that if you go this way, the \, in front of the differential in integrals should be removed.