As long as the houses in a city are contiguous, meaning that they are not farther away from one another than the size of a karpif (a large courtyard, approximately 32 m long), they are considered part of one area for the purpose of assessing the teĥum. If they are separated from one another by more space than that, they are not considered part of one area, and each house’s teĥum Shabbat is then calculated separately.
If the houses in a city are contiguous, then even if one house is out of alignment, as long as it is not more than 32 m from the next house, the teĥum’s square or rectangle expands to include the unaligned house. If there are additional houses after this one, the teĥum expands to include them as well; this can continue even if it means that the teĥum extends outside the city proper for a distance that would take days to walk. As long as each house is not separated from the next by more than 32 m, the teĥum extends to include them. However, if a house is more than 32 m away, it is not included within the rectangle.
If the distance between two adjacent neighborhoods is greater than the size of two large courtyards (i.e., over 64 m), then each neighborhood is considered a town in its own right. We square each on its own, and then 2,000 amot are measured in each direction from that square. In contrast, if the distance separating the two neighborhoods is 64 m or less, they are considered one area and we square them together. There must be at least fifty residents living in an area for a group of houses to be defined as a neighborhood (Eruvin 60a). Even if there are fewer than fifty people, as long as the area contains three courtyards, each of which joins two homes together, or six homes, each of which has a courtyard, the area is still considered a neighborhood (MB 398:38; Ĥazon Ish OĤ 110:19).
If an area is enclosed by a wall or an eruv, all its homes and neighborhoods are considered one area. This is true even when the wall or eruv is farther than a karpif’s length from the last house, and even if there is a large distance between the homes and neighborhoods.