Tab '\t' has inconsistent spacing - c++

I am using \t to space out my output, but it produces inconsistent spaces.
For example, the following code produces
#include <iostream>
int main()
{
std::cout << "Terms\tResults\tet(%)\tea(%)\n";
return 0;
}
Terms Results et(%) ea(%)
Note the difference in space
Why is this so?

Tab ends each 8th column.
v v v v v
1234567812345678123456781234567812345678
Terms Results et(%) ea(%)

The word "tab" is a prefix from "table" or "tabulate". The purpose of tabs is to generate vertical alignment, such as arrangement of text into table columns, or achieving consistent leading indentation for each paragraph. The purpose of tabs isn't to generate equal horizontal spacing. How much space is generated by a tab depends on how close it is to the next tab stop. That's why you're seeing the "difference in space".
The tab character, and its interpretation on terminal devices, originates with the Tab key on typewriters. The Tab key on a typewriter triggers the motion of the carriage, which comes to rest at at the next tab stop. Typewriter tab stops are configurable by the typist by moving mechanical sliders.
On character display devices, the ASCII TAB character works similarly: it advances to the next tab position. Tab stops are commonly every eight characters. (On some terminals they are configurable via menus in the firmware, or even escape sequences that the host computer can generate).
Modern word processing programs still imitate the typewriter tab stop sliders as wedge-shaped elements that can be added to a "ruler" and moved around.
Try this:
std::cout << "Terms\tResults\tet(%)\tea(%)\n";
std<<cout << "a\tb\t\tc\n"
You should see tabular alignment:
Terms Results et(%) ea(%)
a b c
Note we had to use two tabs after b because the Results et ... field overflowed a tab position.
Ideally, we should move the tab stops around based on the width requirements of the columns in our table. But moving tabs stops around is quite non-portable. For this reason, using tabs for vertical alignment in the output of computer programs is basically not very feasible; columnar formatting is better achieved using spaces. A good formatting function can provide for arbitrary field widths, with left, center or right alignment within a field.

Related

Moving over tab spaces in ncurses

I am creating a basic text editor using ncurses. It can display text fine, but navigating with arrow keys causes a problem when tabs are encountered. Calling move(y, x) will freely move the cursor onto a tab space, where most text editors will jump to the next character. Is there functionality within ncurses to jump over tab spaces or do I need to find a way to do it myself?
You have to do it yourself: wmove moves to the given coordinates, ignoring the way characters are displayed on the screen.
If a destructive (filling with spaces) tab works for your application, then you could use waddch:
If ch is a tab, newline, carriage return or backspace, the
cursor is moved appropriately within the window:
Tabs are considered to be at every eighth column. The
tab interval may be altered by setting the TABSIZE
variable.
For an editor, you probably do not want that behavior (though it probably would be helpful to use the TABSIZE feature when displaying text).

Move insertion point to the beginning of the blank line between paragraphs MS Word

I often need to translate a document in MS Word and I do it paragraph by paragraph, the translation text follows each individual paragraph of the original text. What I need is a keyboard shortcut to move the insertion point to the blank space after the following paragraph I need to translate, i.e to move the cursor from the end of the red colored text in the picture to the blank space after the following paragraph ending with "..and call it a day"
Ctrl+Down Arrow shortcut in Word places the insertion point at the beginning of every following paragraph, while I need it placed at the beginning of the blank line above it so I can immediately start typing.
I am looking for a Word shortcut key, regex expression or an autohotkey script that could perform this task, it would come handy to me in doing translation in MS Word.
Thank you for the help!
On MCL's suggestion I've created a simple Autohotkey script to solve the problem, combining Word keyboard shortcuts into one. I also added a code from another script which sets dark red color of the text which is being typed into the original document to make the contrast with the default text color of the original. This is a convenient option for translators which also allows saving only the translated text by using Find function in Word, and removes the need for any further editing of the translated document. Here's the script:
#IfWinActive ahk_class OpusApp
^2::
Send ^{Down}
Send ^{Down}
Send ^{Left}
Send {Enter}
{
oWord := ComObjActive("Word.Application") ; MS Word object
wdColorDarkRed = 128
oWord.Selection.Font.Color := wdColorDarkRed
}
return

Adobe brackets: write spaces instead of tabs

I am using brackets with coffeescript, but when I hit Tab, it insert a tabulation whereas i only need 2 spaces. Also, when I create a line break, the indent is tabs, and not spaces. Can I change these 2 setting ?
In the lower-right of the status bar you should see an indicator saying "Tab Size." Click the label to toggle to spaces. To change the amount of indent, click the number next to it and type a new value.
Note: if the indicator already says "Spaces" then Brackets should be using spaces instead of tab characters already. But it might not feel that way because when you move the cursor or press Backspace, there's a "soft tabs" behavior: the cursor will smartly skips over contiguous spaces to line up evenly with the next tab stop. If that bothers you, there will be a preference in the next release of Brackets (Sprint 38) to disable that behavior, making the cursor never move more than one space at a time.
For language specific control, Brackets allows you to provide different tab and space indentation values in the brackets.json file. For example:
"language": {
"html": {
"spaceUnits": 4
},
"javascript": {
"tabSize": 2
}
}
For changing tab spacing you can edit your brackets.json file. you can find it on Debug->Open Preferences File and simple add "spaceUnits": 2 at the end of the file for 2 space. Remember to add a comma on previous line. You can edit that file for customizing your bracket.
Or you can change it more easily at the right-bottom of your bracket interface you saw a option Space: 4 click on the number and change it as your wish...

how many spaces are considered in \t

why the number of space is different in case 3
how the result is getting effected by \t character.
(-) refers space by (\t)
case 1
void main()
{
int a,b;
printf("%d",printf("hello%d\t",scanf("%d%d",&a,&b)));
}
here the output is>hello2-7
case 2
void main()
{
int a,b;
printf("%d",printf("hello\t%d",scanf("%d%d",&a,&b)));
}
here the output is>hello-27
case 3
void main()
{
int a,b;
printf("%d",printf("\thello%d",scanf("%d%d",&a,&b)));
}
here the output is>--------hello27
Why in the 3rd case there are 8 spaces.
Most terminal programs will have a tab stop at every 8th column - so I'd expect output to be determined like this (I know your output's a little different - discussed below):
. column
. 1 2
input 12345678901234567890
"%d",printf("hello%d\t" hello2__7
"%d",printf("hello\t%d" hello___27
"%d",printf("\thello%d" ________hello27
To understand this, you have to understand the order of evaluation of your (unnecessarily complex) code. Examining the first printf line...
printf("%d",printf("hello%d\t",scanf("%d%d",&a,&b)));
Above, the arguments to the left-hand printf have to be prepared before it can print anything itself, and those arguments include the result of calling the right-hand printf. That right-hand printf outputs hello, the number of arguments scanf read from standard input which is 2 if you typed two, then the tab, then the right-hand printf has finished outputting and returns "7" to indicate how many characters it printed, which is printed by the left-hand printf. I would expect a tab to take you to the 9th column on screen, which suggests TWO spaces before the 7, where-as your question says you're observing 1. Clearly your terminal works a little different, probably considering the 8th, 16th, 24th etc. columns to be tab stops.
More about tabs
There is no universal interpretation of the \t TAB character... how it's rendered depends on the terminal software or rendering device you're using (e.g. an xterm, vt220, vt100 terminal, MS-DOS command window, printer, IDE, text editor etc.).
Some display/printing/formatting programs will consider there to be a tab stop every N characters, where N is often 8, such that if you issue a tab from the first column through to the 8th column you're taken to the 9th, a tab from the 9th to 16th column takes you to the 17th etc.. But, many programs will have ways to set arbitrary columns for tab placements. Some programs like MS Word can use variable-width fonts with which the number of characters between tab stops varies: if your C++ program prints some text that you import into Word you may find it practically impossible to work out how many tabs are needed to get the desired alignment of output - it's generally easier to just put one tab between values and change your tab stops inside Word so it all looks ok, or stick to a fixed-width font such as Courier.
C++ IDEs often let you set the value ("N" above) for columns per tab stop - 4 and 8 are both common settings, with 8 often meaning your source code indentation is a mix of tabs and spaces to reach the desired left-hand-column: that's kind of messy to navigate with naive cursor movement implementation. Many people prefer to set a "insert spaces when tab is pressed" option so the file is always saved with actual spaces, and displays more predictably with a wide variety of display/printing software.
A TAB has only the space given to it as it is rendered (so does any character, really); however, one subtle difference with tabs is that they are often taken to mean advance to the next "virtual column" (I'm sure there is a better term), where these virtual columns are, say, 8 characters wide; although this width can often be changed.
Here is an ugly graphic, where n..- represents a "virtual column" and T..t represents the space "taken up" by the tab:
1-------2-------3-------
hello\tworld helloTttworld
\thelloworld Tttttttthelloworld
hello\t\tworld helloTttTtttttttworld
in C99 and C11,
\t ( horizontal tab ) Moves the active position to the next horizontal
tabulation position on the current line. If the active position is at
or past the last defined horizontal tabulation position, the behavior
of the display device is unspecified.
while C++03 and C++11 don't specify the difference of '\t' with C.
\t does not contain any space. \t is a proper character that could be displayed with different length, but it's only one char.
As per compiler and computer software width would be changing either 4bits or 8 bits For example: here is c program compiled using compiler gcc 6.3 compiler on windows 10 pro where width 4 bits has taken,
#include <stdio.h>
int main(void) {
printf("a12345678patil\n");
printf("a\tpat\til");
}
output:
a12345678patil
a pat il

How do tabs work for text boxes?

I'm making a game gui api and I'm wondering how to implement tabs. I'm using freetype for text. When I try to render '\t' It looks like a square. I'm wondering how tabs are implemented because they are not a fixed width.
Thanks
For a fixed-width font you could compute how many spaces to the next tab stop, but the general solution is to stop rendering when you hit a tab, move to the next tabstop, and then render the text that comes after the tab character starting from there. Where the tabstops are is up to you, but a good default is probably something like every 8 ems.
The simplest strategy is to swap a tab out for a some number of spaces. I.e. when the user pushes tab, pretend they pushed tab four times instead. (Or just print out four spaces, whatever works for you)