enter image description here

For example, as in the picture. So that when you click on the button above the selected element (or group of elements) there appears a negation sign (double / triple). If it cannot be implemented in these languages, then there may be at least some other ideas ..

  • And if you divide this condition on the grid? - Egor Randomize
  • 2
    Look towards controls for visualizing LATEX. I tried the WPF-Math library, but it doesn’t seem to support \overline . Association: stackoverflow.com/q/8899204/6766879 - Andrew NOP

1 answer 1

Well, you can do it, although it is much less convenient than in TeX. You will have to measure characters yourself and work with font properties. Here you have a simple option: it does not take into account italics and lowercase letters.

XAML:

 <Window x:Class="Test.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="ВСст" Height="350" Width="525" TextBlock.FontSize="15"> <Grid> <Grid HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="Host" ClipToBounds="False"> <TextBlock x:Name="Target"/> </Grid> </Grid> </Window> 

Let's start a small auxiliary class:

 class LevelDescriptor { public double Y; public List<(double from, double to)> Lines; } 

Our code:

 string text = "(X & Y) & (X & Y) & (X & Z)"; // описаниС Π»ΠΈΠ½ΠΈΠΉ var lines = new List<List<(int from, int to)>>() { new List<(int from, int to)>() { (1, 2), (15, 16), (25, 26) }, // ΡƒΡ€ΠΎΠ²Π΅Π½ΡŒ 1 new List<(int from, int to)>() { (1, 6), (11, 16), (21, 26) }, // ΡƒΡ€ΠΎΠ²Π΅Π½ΡŒ 2 new List<(int from, int to)>() { (0, 27) }, // ΡƒΡ€ΠΎΠ²Π΅Π½ΡŒ 3 }; // Π²Π΅Ρ€Ρ‚ΠΈΠΊΠ°Π»ΡŒΠ½ΠΎΠ΅ расстояниС ΠΌΠ΅ΠΆΠ΄Ρƒ линиями const double vgap = 4; // Ρ€Π°Π·Ρ€Π΅ΡˆΠ΅Π½ΠΈΠ΅ вашСго ΠΌΠΎΠ½ΠΈΡ‚ΠΎΡ€Π°. (Ссли ΠΎΠ½ΠΎ измСняСтся, Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€, Ρƒ вас ΠΈΡ… нСсколько, Ρ‚ΠΎ // Π½Π°Π΄ΠΎ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Ρ‚ΡŒΡΡ Π½Π° Π΅Π³ΠΎ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠ΅) var pixelsPerDip = VisualTreeHelper.GetDpi(this).PixelsPerDip; // ΠΊΡƒΠ΄Π° пишСм TextBlock tb = Target; tb.Text = text; // ΠΎΡ‚Π±ΠΈΡ€Π°Π΅ΠΌ Ρƒ тСкстбокса ΡˆΡ€ΠΈΡ„Ρ‚ var font = new Typeface(tb.FontFamily, tb.FontStyle, tb.FontWeight, tb.FontStretch); // Ρ„ΠΎΡ€ΠΌΠ°Ρ‚ΠΈΡ€ΡƒΠ΅ΠΌ тСкст FormattedText formattedText = new FormattedText( text, CultureInfo.GetCultureInfo("en-US"), FlowDirection.LeftToRight, font, tb.FontSize, Brushes.Black, // конкрСтная ΠΊΠΈΡΡ‚ΡŒ Π½Π°ΠΌ Π½Π΅ Π²Π°ΠΆΠ½Π°, ΠΌΡ‹ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅ΠΌ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Π³Π΅ΠΎΠΌΠ΅Ρ‚Ρ€ΠΈΡŽ pixelsPerDip); // Π½Π° ΠΊΠ°ΠΊΠΎΠΉ высотС начинаСтся большая Π±ΡƒΠΊΠ²Π°? double vstart = formattedText.Baseline - font.CapsHeight * tb.FontSize; // описатСли Π»ΠΈΠ½ΠΈΠΉ List<LevelDescriptor> result = new List<LevelDescriptor>(); // тСкущая высота double height = vstart; foreach (var line in lines) { // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ ΠΎΠΊΡ€ΡƒΠΆΠ°ΡŽΡ‰ΠΈΠ΅ ΠΏΡ€ΡΠΌΠΎΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊΠΈ для всСх подстрок var boundingBoxes = line.Select(d => formattedText.BuildHighlightGeometry(new Point(), d.from, d.to - d.from).Bounds); height -= vgap; // Π·Π°ΠΏΠΎΠΌΠ½ΠΈΠ»ΠΈ высоту, ΠΈ Π»Π΅Π²ΡƒΡŽ/ΠΏΡ€Π°Π²ΡƒΡŽ Π³Ρ€Π°Π½ΠΈΡ†Ρƒ для ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΠΎΡ‚Ρ€Π΅Π·ΠΊΠ° result.Add(new LevelDescriptor() { Y = height, Lines = boundingBoxes.Select(bb => (from: bb.Left, to: bb.Right)).ToList() }); } // Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ создаём Π»ΠΈΠ½ΠΈΠΈ var brush = tb.Foreground; // Ρ†Π²Π΅Ρ‚ Ρ‚Π°ΠΊΠΎΠΉ ΠΆΠ΅, ΠΊΠ°ΠΊ ΠΈ Ρƒ строки foreach (var level in result) { foreach (var d in level.Lines) Host.Children.Add( new Line() { X1 = d.from, X2 = d.to, Y1 = level.Y, Y2 = level.Y, StrokeThickness = 1, // Ρ‚ΠΎΠ»Ρ‰ΠΈΠ½Π° Stroke = brush, Fill = brush }); } 

That's all. You will have to build your own lines table based on your data.

I got this result here:

Yes, I know that TeX is cooler, but WPF can also be in capable hands

  • Can you please still help: how to put a negative on the selected text, for example, by pressing a button? If possible, show this code? - Anastasia
  • @ Anastasia: But this is more difficult. If you need to display not in TextBlock 'e, but in TextBox ' e (you can only select text there), my code will not work. Plus, you will have to get the indices of the selected elements (this is simple), and check them for meaningfulness, for example, so that the line does not end in the middle of the formula (this is difficult). - VladD
  • and other options, where and how to implement it exist? - Anastasia
  • @ Anastasia: Well, hmm. It depends on what you need. Where exactly are you going to show the text? In TextBox 'e? In TextBlock 'e? Somewhere else? - VladD
  • I want to Textbox. - Anastasia