From 186e0c8561cc908c2974ded4a42c0e8e65cbad79 Mon Sep 17 00:00:00 2001 From: d-buchmann Date: Tue, 1 Jul 2025 14:36:31 +0200 Subject: [PATCH] Update AbstractParameter.php Make lazy null conditionals explicit. Try to handle LaTeX special chars gracefully. Fixes #958 --- src/Entity/Parameters/AbstractParameter.php | 28 ++++++++++++++++----- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/Entity/Parameters/AbstractParameter.php b/src/Entity/Parameters/AbstractParameter.php index edcedc3e..eee0a030 100644 --- a/src/Entity/Parameters/AbstractParameter.php +++ b/src/Entity/Parameters/AbstractParameter.php @@ -217,7 +217,7 @@ abstract class AbstractParameter extends AbstractNamedDBElement implements Uniqu $str = ''; $bracket_opened = false; - if ($this->value_typical) { + if ($this->value_typical !== null) { $str .= $this->getValueTypicalWithUnit($latex_formatted); if ($this->value_min || $this->value_max) { $bracket_opened = true; @@ -225,11 +225,11 @@ abstract class AbstractParameter extends AbstractNamedDBElement implements Uniqu } } - if ($this->value_max && $this->value_min) { + if ($this->value_max !== null && $this->value_min !== null) { $str .= $this->getValueMinWithUnit($latex_formatted).' ... '.$this->getValueMaxWithUnit($latex_formatted); - } elseif ($this->value_max) { + } elseif ($this->value_max !== null) { $str .= 'max. '.$this->getValueMaxWithUnit($latex_formatted); - } elseif ($this->value_min) { + } elseif ($this->value_min !== null) { $str .= 'min. '.$this->getValueMinWithUnit($latex_formatted); } @@ -449,7 +449,15 @@ abstract class AbstractParameter extends AbstractNamedDBElement implements Uniqu if (!$with_latex) { $unit = $this->unit; } else { - $unit = '$\mathrm{'.$this->unit.'}$'; + // if chars occur that will mess up the latex formatting, display them plain, regardless of the desired output + if (str_contains($this->unit, '~') || str_contains($this->unit, '^') || str_contains($this->unit, '\\')) + { + $unit = $this->unit; + } + else // otherwise, style the chars with latex, but escape latex special chars + { + $unit = '$\mathrm{'.$this->latexEscape($this->unit).'}$'; + } } return $str.' '.$unit; @@ -457,7 +465,15 @@ abstract class AbstractParameter extends AbstractNamedDBElement implements Uniqu return $str; } - + + /** + * Return a latex escaped string + */ + protected function latexEscape(string $latex): string + { + return preg_replace('/(\&|\%|\$|\#|\_|\{|\})/', "\\\\$1", $latex); + } + /** * Returns the class of the element that is allowed to be associated with this attachment. * @return string