How Many Spaces for Tab Character(\T)

How many spaces for tab character(\t)?

A tab character should advance to the next tab stop. Historically tab stops were every 8th character, although smaller values are in common use today and most editors can be configured.

I would expect your output to look like the following:

123456789
a b
c

The algorithm is to start a column count at zero, then increment it for each character output. When you get to a tab, output n-(c%n) spaces where c is the column number (zero based) and n is the tab spacing.

how many spaces are considered in \t

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.

Length of tab character

It is not possible. But, you can replace every tab with custom amounts of spaces using str.expandtabs:

print repr('test\ttest 2'.expandtabs())
# output: 'test test 2'

print repr('test\ttest 2'.expandtabs(2))
# output: 'test test

Edit: note that when using str.expandtabs, the width of tab will depend on where in string the tab is:

print repr('test\ttest 2'.expandtabs(8))
print repr('tessst\ttest 2'.expandtabs(8))
# output: 'test test 2'
# 'tessst test 2'

If you want each tab to be replaced by specifyed number of spaces, you can use str.replace:

print repr('test\ttest 2'.replace('\t', ' ' * 8))
print repr('tessst\ttest 2'.replace('\t', ' ' * 8))
# output: 'test test 2'
# 'tessst test 2'

What do \t and \b do?

Backspace and tab both move the cursor position. Neither is truly a 'printable' character.

Your code says:

  1. print "foo"
  2. move the cursor back one space
  3. move the cursor forward to the next tabstop
  4. output "bar".

To get the output you expect, you need printf("foo\b \tbar"). Note the extra 'space'. That says:

  1. output "foo"
  2. move the cursor back one space
  3. output a ' ' (this replaces the second 'o').
  4. move the cursor forward to the next tabstop
  5. output "bar".

Most of the time it is inappropriate to use tabs and backspace for formatting your program output. Learn to use printf() formatting specifiers. Rendering of tabs can vary drastically depending on how the output is viewed.

This little script shows one way to alter your terminal's tab rendering. Tested on Ubuntu + gnome-terminal:

#!/bin/bash
tabs -8
echo -e "\tnormal tabstop"
for x in `seq 2 10`; do
tabs $x
echo -e "\ttabstop=$x"
done

tabs -8
echo -e "\tnormal tabstop"

Also see man setterm and regtabs.

And if you redirect your output or just write to a file, tabs will quite commonly be displayed as fewer than the standard 8 chars, especially in "programming" editors and IDEs.

So in otherwords:

printf("%-8s%s", "foo", "bar"); /* this will ALWAYS output "foo     bar" */
printf("foo\tbar"); /* who knows how this will be rendered */

IMHO, tabs in general are rarely appropriate for anything. An exception might be generating output for a program that requires tab-separated-value input files (similar to comma separated value).

Backspace '\b' is a different story... it should never be used to create a text file since it will just make a text editor spit out garbage. But it does have many applications in writing interactive command line programs that cannot be accomplished with format strings alone. If you find yourself needing it a lot, check out "ncurses", which gives you much better control over where your output goes on the terminal screen. And typically, since it's 2011 and not 1995, a GUI is usually easier to deal with for highly interactive programs. But again, there are exceptions. Like writing a telnet server or console for a new scripting language.

How to substitude all \t (tab characters) with white space in a PDF

I doubt that the 'tabs' are contained in the PDF as tabs. The 'tab' character (0x04 in ASCII) has no special significance in PDF, and in particular it does not move the current point, it simply draws a glyph. As a result, if you do (A\tB) what you will see when the PDF is rendered is 'AB'. Or 'A*B' where the * is some other character you didn't expect (often a square)

So you would probably actually have to convert current point movement operators into white space drawing There's no realistic way that can be automated, since no tool can tell where a movement was a 'tab' and where it was a reposition.

So you will need to do it manually.

The challenge here is that the page content stream is likely to be compressed, so the first thing you will have to do is decompress the PDF. There are a number of tools which will do this for you, MuPDF is one, I think pdftk is another.

Then you will need to locate the position where you want to insert space, this could be challenging, as the font may be re-encoded to something other than ASCII so it may be hard to identify the correct position. Once you've done that, you can insert the space(s) you want into the text strings, again bearing in mind that the font in use may be re-encoded, and subset. This means that a space may not be 0x20 and indeed the font may not even contain a space glyph. And of course you need to remove the operations to reposition the current point.

Finally, after you've modified the contents, you need to remember that PDF is a binary format, and the xref table contains the position of every element in the file. If you've edited the file its likely that you will have altered the length of one or more elements, which will change the offset of any following elements, so you will need to recalculate those and update the xref table.

I suspect you are going to find it easier to modify the conversion from PDF to HTML, or modify the HTML, than to try and alter the PDF file.

Redefine tab as 4 spaces

It depends on what you mean. Do you want actual tab characters in your file to appear 4 spaces wide, or by "tab" do you actually mean an indent, generated by pressing the tab key, which would result in the file literally containing (up to) 4 space characters for each "tab" you type?

Depending on your answer, one of the following sets of
settings should work for you:

  • For tab characters that appear 4-spaces-wide:

    set tabstop=4

    If you're using actual tab character in your source code you probably also want these settings (these are actually the defaults, but you may want to set them defensively):

    set softtabstop=0 noexpandtab

    Finally, if you want an indent to correspond to a single tab, you should also use:

    set shiftwidth=4
  • For indents that consist of 4 space characters but are entered with the tab key:

    set tabstop=8 softtabstop=0 expandtab shiftwidth=4 smarttab

To make the above settings permanent add
these lines to your vimrc.

In case you need to make adjustments, or would simply like to understand what these options all mean, here's a breakdown of what each option means:

tabstop


The width of a hard tabstop measured in "spaces" -- effectively the (maximum) width of an actual tab character.

shiftwidth


The size of an "indent". It's also measured in spaces, so if your code base indents with tab characters then you want shiftwidth to equal the number of tab characters times tabstop. This is also used by things like the =, > and < commands.

softtabstop


Setting this to a non-zero value other than tabstop will make the tab key (in insert mode)
insert a combination of spaces (and possibly tabs) to simulate tab stops at this width.

expandtab


Enabling this will make the tab key (in insert mode) insert spaces instead of
tab characters. This also affects the behavior of the retab command.

smarttab


Enabling this will make the tab key (in insert mode) insert spaces or tabs to
go to the next indent
of the next tabstop when the cursor is at the beginning of a line (i.e. the
only preceding characters are whitespace).

For more details on any of these see :help 'optionname' in vim (e.g. :help 'tabstop')

Why \t in c++ have different width?

It's not C++ depending. Tabulation representation size depends of the position on the line to the next Tab stop

Tab_key

Tab_stop

How do I write a tab in Python?

This is the code:

f = open(filename, 'w')
f.write("hello\talex")

The \t inside the string is the escape sequence for the horizontal tabulation.



Related Topics



Leave a reply



Submit