Skip to content

Commit 4e1b1a4

Browse files
committed
Render units of parameters in upstanding latex
Fixes issue #856
1 parent 5b111d8 commit 4e1b1a4

File tree

6 files changed

+45
-18
lines changed

6 files changed

+45
-18
lines changed

assets/controllers/pages/latex_preview_controller.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,20 @@ import "katex/dist/katex.css";
2525
export default class extends Controller {
2626
static targets = ["input", "preview"];
2727

28+
static values = {
29+
unit: {type: Boolean, default: false} //Render as upstanding (non-italic) text, useful for units
30+
}
31+
2832
updatePreview()
2933
{
30-
katex.render(this.inputTarget.value, this.previewTarget, {
34+
let value = "";
35+
if (this.unitValue) {
36+
value = "\\mathrm{" + this.inputTarget.value + "}";
37+
} else {
38+
value = this.inputTarget.value;
39+
}
40+
41+
katex.render(value, this.previewTarget, {
3142
throwOnError: false,
3243
});
3344
}

assets/css/app/helpers.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,11 @@ ul.structural_link li a:hover {
111111
.permission-checkbox:checked {
112112
background-color: var(--bs-success);
113113
border-color: var(--bs-success);
114+
}
115+
116+
/***********************************************
117+
* Katex rendering with same height as text
118+
***********************************************/
119+
.katex-same-height-as-text .katex {
120+
font-size: 1.0em;
114121
}

src/Entity/Parameters/AbstractParameter.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public function getElement(): ?AbstractDBElement
208208
*/
209209
#[Groups(['parameter:read', 'full'])]
210210
#[SerializedName('formatted')]
211-
public function getFormattedValue(): string
211+
public function getFormattedValue(bool $latex_formatted = false): string
212212
{
213213
//If we just only have text value, return early
214214
if (null === $this->value_typical && null === $this->value_min && null === $this->value_max) {
@@ -218,19 +218,19 @@ public function getFormattedValue(): string
218218
$str = '';
219219
$bracket_opened = false;
220220
if ($this->value_typical) {
221-
$str .= $this->getValueTypicalWithUnit();
221+
$str .= $this->getValueTypicalWithUnit($latex_formatted);
222222
if ($this->value_min || $this->value_max) {
223223
$bracket_opened = true;
224224
$str .= ' (';
225225
}
226226
}
227227

228228
if ($this->value_max && $this->value_min) {
229-
$str .= $this->getValueMinWithUnit().' ... '.$this->getValueMaxWithUnit();
229+
$str .= $this->getValueMinWithUnit($latex_formatted).' ... '.$this->getValueMaxWithUnit($latex_formatted);
230230
} elseif ($this->value_max) {
231-
$str .= 'max. '.$this->getValueMaxWithUnit();
231+
$str .= 'max. '.$this->getValueMaxWithUnit($latex_formatted);
232232
} elseif ($this->value_min) {
233-
$str .= 'min. '.$this->getValueMinWithUnit();
233+
$str .= 'min. '.$this->getValueMinWithUnit($latex_formatted);
234234
}
235235

236236
//Add closing bracket
@@ -344,25 +344,25 @@ public function getValueTypical(): ?float
344344
/**
345345
* Return a formatted version with the minimum value with the unit of this parameter.
346346
*/
347-
public function getValueTypicalWithUnit(): string
347+
public function getValueTypicalWithUnit(bool $with_latex = false): string
348348
{
349-
return $this->formatWithUnit($this->value_typical);
349+
return $this->formatWithUnit($this->value_typical, with_latex: $with_latex);
350350
}
351351

352352
/**
353353
* Return a formatted version with the maximum value with the unit of this parameter.
354354
*/
355-
public function getValueMaxWithUnit(): string
355+
public function getValueMaxWithUnit(bool $with_latex = false): string
356356
{
357-
return $this->formatWithUnit($this->value_max);
357+
return $this->formatWithUnit($this->value_max, with_latex: $with_latex);
358358
}
359359

360360
/**
361361
* Return a formatted version with the typical value with the unit of this parameter.
362362
*/
363-
public function getValueMinWithUnit(): string
363+
public function getValueMinWithUnit(bool $with_latex = false): string
364364
{
365-
return $this->formatWithUnit($this->value_min);
365+
return $this->formatWithUnit($this->value_min, with_latex: $with_latex);
366366
}
367367

368368
/**
@@ -441,11 +441,18 @@ public function setValueText(string $value_text): self
441441
/**
442442
* Return a string representation and (if possible) with its unit.
443443
*/
444-
protected function formatWithUnit(float $value, string $format = '%g'): string
444+
protected function formatWithUnit(float $value, string $format = '%g', bool $with_latex = false): string
445445
{
446446
$str = sprintf($format, $value);
447447
if ($this->unit !== '') {
448-
return $str.' '.$this->unit;
448+
449+
if (!$with_latex) {
450+
$unit = $this->unit;
451+
} else {
452+
$unit = '$\mathrm{'.$this->unit.'}$';
453+
}
454+
455+
return $str.' '.$unit;
449456
}
450457

451458
return $str;

templates/form/filter_types_layout.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
<td class="col-sm-2">{{ form_widget(form.name, {"attr": {"data-pages--parameters-autocomplete-target": "name"}}) }}</td>
5555
<td {{ stimulus_controller('pages/latex_preview') }}>{{ form_widget(form.symbol, {"attr": {"data-pages--parameters-autocomplete-target": "symbol", "data-pages--latex-preview-target": "input"}}) }}<span {{ stimulus_target('pages/latex_preview', 'preview') }}></span></td>
5656
<td>{{ form_widget(form.value) }}</td>
57-
<td {{ stimulus_controller('pages/latex_preview') }}>{{ form_widget(form.unit, {"attr": {"data-pages--parameters-autocomplete-target": "unit", "data-pages--latex-preview-target": "input"}}) }}<span {{ stimulus_target('pages/latex_preview', 'preview') }}></span></td>
57+
<td {{ stimulus_controller('pages/latex_preview', {"unit": true}) }}>{{ form_widget(form.unit, {"attr": {"data-pages--parameters-autocomplete-target": "unit", "data-pages--latex-preview-target": "input"}}) }}<span {{ stimulus_target('pages/latex_preview', 'preview') }}></span></td>
5858
<td>{{ form_widget(form.value_text) }}</td>
5959
<td>
6060
<button type="button" class="btn btn-danger btn-sm" {{ collection.delete_btn() }} title="{% trans %}orderdetail.delete{% endtrans %}">

templates/helper.twig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,16 @@
218218
<thead>
219219
<tr>
220220
<th>{% trans %}specifications.property{% endtrans %}</th>
221+
<th>{% trans %}specifications.symbol{% endtrans %}</th>
221222
<th>{% trans %}specifications.value{% endtrans %}</th>
222223
</tr>
223224
</thead>
224225
<tbody>
225226
{% for param in parameters %}
226227
<tr>
227-
<td>{{ param.name }} {% if param.symbol is not empty %}<span class="latex" data-controller="common--latex">${{ param.symbol }}$</span>{% endif %}</td>
228-
<td>{{ param.formattedValue }}</td>
228+
<td>{{ param.name }}</td>
229+
<td>{% if param.symbol is not empty %}<span class="latex" {{ stimulus_controller('common/latex') }}>${{ param.symbol }}$</span>{% endif %}</td>
230+
<td {{ stimulus_controller('common/latex') }} class="katex-same-height-as-text">{{ param.formattedValue(true) }}</td>
229231
</tr>
230232
{% endfor %}
231233
</tbody>

templates/parts/edit/edit_form_styles.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
<td>{{ form_widget(form.value_min) }}{{ form_errors(form.value_min) }}</td>
7676
<td>{{ form_widget(form.value_typical) }}{{ form_errors(form.value_typical) }}</td>
7777
<td>{{ form_widget(form.value_max) }}{{ form_errors(form.value_max) }}</td>
78-
<td {{ stimulus_controller('pages/latex_preview') }}>{{ form_widget(form.unit, {"attr": {"data-pages--parameters-autocomplete-target": "unit", "data-pages--latex-preview-target": "input"}}) }}{{ form_errors(form.unit) }}<span {{ stimulus_target('pages/latex_preview', 'preview') }}></span></td>
78+
<td {{ stimulus_controller('pages/latex_preview', {"unit": true}) }}>{{ form_widget(form.unit, {"attr": {"data-pages--parameters-autocomplete-target": "unit", "data-pages--latex-preview-target": "input"}}) }}{{ form_errors(form.unit) }}<span {{ stimulus_target('pages/latex_preview', 'preview') }}></span></td>
7979
<td>{{ form_widget(form.value_text) }}{{ form_errors(form.value_text) }}</td>
8080
<td>{{ form_widget(form.group) }}{{ form_errors(form.group) }}</td>
8181
<td>

0 commit comments

Comments
 (0)