Skip to content

Commit 2226b72

Browse files
Update AbstractParameter.php (#959)
* Update AbstractParameter.php Make lazy null conditionals explicit. Try to handle LaTeX special chars gracefully. Fixes #958 * Only escape the percentage sign, so that you can still use latex for units * Only escape previously unescaped percentage signs * simplify regex * Render the percentage sign correctly in units in the frontend. --------- Co-authored-by: Jan Böhmer <[email protected]>
1 parent 00a74ed commit 2226b72

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

assets/controllers/pages/latex_preview_controller.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ export default class extends Controller {
3333
{
3434
let value = "";
3535
if (this.unitValue) {
36-
value = "\\mathrm{" + this.inputTarget.value + "}";
36+
//Escape percentage signs
37+
value = this.inputTarget.value.replace(/%/g, '\\%');
38+
39+
value = "\\mathrm{" + value + "}";
3740
} else {
3841
value = this.inputTarget.value;
3942
}

assets/controllers/pages/parameters_autocomplete_controller.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ export default class extends Controller
8585
tmp += '<span>' + katex.renderToString(data.symbol) + '</span>'
8686
}
8787
if (data.unit) {
88-
tmp += '<span class="ms-2">' + katex.renderToString('[' + data.unit + ']') + '</span>'
88+
let unit = data.unit.replace(/%/g, '\\%');
89+
unit = "\\mathrm{" + unit + "}";
90+
tmp += '<span class="ms-2">' + katex.renderToString('[' + unit + ']') + '</span>'
8991
}
9092

9193

src/Entity/Parameters/AbstractParameter.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,19 +217,19 @@ public function getFormattedValue(bool $latex_formatted = false): string
217217

218218
$str = '';
219219
$bracket_opened = false;
220-
if ($this->value_typical) {
220+
if ($this->value_typical !== null) {
221221
$str .= $this->getValueTypicalWithUnit($latex_formatted);
222222
if ($this->value_min || $this->value_max) {
223223
$bracket_opened = true;
224224
$str .= ' (';
225225
}
226226
}
227227

228-
if ($this->value_max && $this->value_min) {
228+
if ($this->value_max !== null && $this->value_min !== null) {
229229
$str .= $this->getValueMinWithUnit($latex_formatted).' ... '.$this->getValueMaxWithUnit($latex_formatted);
230-
} elseif ($this->value_max) {
230+
} elseif ($this->value_max !== null) {
231231
$str .= 'max. '.$this->getValueMaxWithUnit($latex_formatted);
232-
} elseif ($this->value_min) {
232+
} elseif ($this->value_min !== null) {
233233
$str .= 'min. '.$this->getValueMinWithUnit($latex_formatted);
234234
}
235235

@@ -449,15 +449,18 @@ protected function formatWithUnit(float $value, string $format = '%g', bool $wit
449449
if (!$with_latex) {
450450
$unit = $this->unit;
451451
} else {
452-
$unit = '$\mathrm{'.$this->unit.'}$';
452+
//Escape the percentage sign for convenience (as latex uses it as comment and it is often used in units)
453+
$escaped = preg_replace('/\\\\?%/', "\\\\%", $this->unit);
454+
455+
$unit = '$\mathrm{'.$escaped.'}$';
453456
}
454457

455458
return $str.' '.$unit;
456459
}
457460

458461
return $str;
459462
}
460-
463+
461464
/**
462465
* Returns the class of the element that is allowed to be associated with this attachment.
463466
* @return string

0 commit comments

Comments
 (0)